shithub: s3

ref: d8adc268f3dccd53e67adc2c7957309db795f6fe
dir: /cmd.c/

View raw version
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "s3.h"

void
download(S3 *s3, char *path, Biobuf *local)
{
	int bfd;
	long n;
	char data[8192];

	bfd = s3get(s3, path);
	if(bfd < 0)
		sysfatal("s3get: %r");
	for(;;){
		n = read(bfd, data, sizeof data);
		if(n < 0)
			sysfatal("download body: %r");
		if(n == 0)
			return;
		Bwrite(local, data, n);
	}
}

int
parseuri(S3 *s3, char *path, int npath, char *arg)
{
	char *p;

	if(strstr(arg, "s3://") != arg)
		return -1;
	arg+=5;
	p = strchr(arg, '/');
	if(p == nil || p == arg)
		return -1;
	snprint(path, npath, "%s", p+1);
	s3->bucket = strdup(arg);
	s3->bucket[p-arg] = 0;
	return 0;
}

int
parseargs(S3 *s3, int argc, char **argv)
{
	extern _Noreturn void usage(void);
	int initial;

	initial = argc;
	s3->access = s3->endpoint = s3->region = nil;
	ARGBEGIN{
	case 'a':
		s3->access = strdup(EARGF(usage()));
		break;
	case 'u':
		s3->endpoint = strdup(EARGF(usage()));
		break;
	case 'r':
		s3->region = strdup(EARGF(usage()));
		break;
	}ARGEND

	if(s3->access == nil)
		s3->access = getenv("AWS_ACCESS_KEY_ID");
	if(s3->endpoint == nil)
		s3->endpoint = getenv("AWS_ENDPOINT_URL_S3");
	if(s3->region)
		s3->region = getenv("AWS_DEFAULT_REGION");

	if(s3->access == nil || s3->endpoint == nil)
		usage();
	if(s3->region == nil)
		s3->region = strdup("auto");

	s3->host = strstr(s3->endpoint, "://");
	if(s3->host == nil)
		sysfatal("invalid endpoint url");
	s3->host += 3;
	return initial-argc;
}