Ejemplo n.º 1
0
// PutS3File writes the specified file to an s3 bucket using the given permissions and content type.
// The details of where to put the file are included in the s3URL
func PutS3File(pushAuth *aws.Auth, localFilePath, s3URL, contentType, permissionACL string) error {
	urlParsed, err := url.Parse(s3URL)
	if err != nil {
		return err
	}

	if urlParsed.Scheme != "s3" {
		return fmt.Errorf("Don't know how to use URL with scheme %v", urlParsed.Scheme)
	}

	localFileReader, err := os.Open(localFilePath)
	if err != nil {
		return err
	}

	fi, err := os.Stat(localFilePath)
	if err != nil {
		return err
	}

	session := NewS3Session(pushAuth, aws.USEast)
	bucket := session.Bucket(urlParsed.Host)
	// options for the header
	options := s3.Options{}
	err = bucket.PutReader(urlParsed.Path, localFileReader, fi.Size(), contentType, s3.ACL(permissionACL), options)
	if err != nil {
		return err
	}
	return nil
}
Ejemplo n.º 2
0
// Put the specified resource to s3.
func (self *S3PutCommand) Put() error {

	fi, err := os.Stat(self.LocalFile)
	if err != nil {
		return err
	}

	fileReader, err := os.Open(self.LocalFile)
	if err != nil {
		return err
	}
	defer fileReader.Close()

	// get the appropriate session and bucket
	auth := &aws.Auth{
		AccessKey: self.AwsKey,
		SecretKey: self.AwsSecret,
	}
	session := thirdparty.NewS3Session(auth, aws.USEast)
	bucket := session.Bucket(self.Bucket)

	options := s3.Options{}
	// put the data
	return bucket.PutReader(
		self.RemoteFile,
		fileReader,
		fi.Size(),
		self.ContentType,
		s3.ACL(self.Permissions),
		options,
	)

}
Ejemplo n.º 3
0
func NewS3Connection(bucket string, region string, public bool) *S3 {
	var acl s3.ACL
	var auth aws.Auth
	var err error

	// set auth
	auth, err = aws.EnvAuth()
	if err != nil {
		auth, err = aws.SharedAuth()
		if err != nil {
			log.Fatal(err)
		}
	}

	// set region
	if region == "" {
		region = os.Getenv("AWS_REGION")
		if region == "" {
			region = "us-east-1"
		}
	}
	awsRegion := aws.Regions[region]

	acl = s3.ACL("private")
	if public {
		acl = s3.ACL("public-read")
	}

	// establish connection
	conn := s3.New(auth, awsRegion)

	// set bucket
	bkt := conn.Bucket(bucket)

	return &S3{
		Auth:   auth,
		Region: awsRegion,
		bucket: bkt,
		conn:   conn,
		ACL:    acl,
	}
}
Ejemplo n.º 4
0
func validS3Permissions(perm string) bool {
	return util.SliceContains(
		[]s3.ACL{
			s3.Private,
			s3.PublicRead,
			s3.PublicReadWrite,
			s3.AuthenticatedRead,
			s3.BucketOwnerRead,
			s3.BucketOwnerFull,
		},
		s3.ACL(perm),
	)
}
Ejemplo n.º 5
0
// PUT on a bucket creates the bucket.
// http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketPUT.html
func (r bucketResource) put(a *action) interface{} {
	var created bool
	if r.bucket == nil {
		if !validBucketName(r.name) {
			fatalf(400, "InvalidBucketName", "The specified bucket is not valid")
		}
		if loc := locationConstraint(a); loc == "" {
			fatalf(400, "InvalidRequets", "The unspecified location constraint is incompatible for the region specific endpoint this request was sent to.")
		}
		// TODO validate acl
		r.bucket = &bucket{
			name: r.name,
			// TODO default acl
			objects: make(map[string]*object),
		}
		a.srv.buckets[r.name] = r.bucket
		created = true
	}
	if !created && a.srv.config.send409Conflict() {
		fatalf(409, "BucketAlreadyOwnedByYou", "Your previous request to create the named bucket succeeded and you already own it.")
	}
	r.bucket.acl = s3.ACL(a.req.Header.Get("x-amz-acl"))
	return nil
}
Ejemplo n.º 6
0
// PutToMyBucket put a file to a bucket.
func PutToMyBucket(prefix string, keyname string, wb *bytes.Buffer, contentType string, aclType string) error {
	acl := s3.ACL(aclType)
	return MyBucket().Put(prefix+keyname, wb.Bytes(), contentType, acl, s3.Options{})
}