예제 #1
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,
	)

}
예제 #2
0
func getS3FileBytes(auth *aws.Auth, bucket string, path string) ([]byte, error) {
	session := thirdparty.NewS3Session(auth, aws.USEast)
	s3bucket := session.Bucket(bucket)
	reader, err := s3bucket.GetReader(path)
	if err != nil {
		return nil, err
	}
	return ioutil.ReadAll(reader)
}
예제 #3
0
// Fetch the specified resource from s3.
func (self *S3GetCommand) Get() error {

	// 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)

	// get a reader for the bucket
	reader, err := bucket.GetReader(self.RemoteFile)
	if err != nil {
		return fmt.Errorf("error getting bucket reader for file %v: %v",
			self.RemoteFile, err)
	}
	defer reader.Close()

	// either untar the remote, or just write to a file
	if self.LocalFile != "" {

		// remove the file, if it exists
		exists, err := util.FileExists(self.LocalFile)
		if err != nil {
			return fmt.Errorf("error checking existence of local file %v: %v",
				self.LocalFile, err)
		}
		if exists {
			if err := os.RemoveAll(self.LocalFile); err != nil {
				return fmt.Errorf("error clearing local file %v: %v",
					self.LocalFile, err)
			}
		}

		// open the local file
		file, err := os.Create(self.LocalFile)
		if err != nil {
			return fmt.Errorf("error opening local file %v: %v", self.LocalFile,
				err)
		}
		defer file.Close()

		_, err = io.Copy(file, reader)
		return err

	} else {

		// wrap the reader in a gzip reader and a tar reader
		gzipReader, err := gzip.NewReader(reader)
		if err != nil {
			return fmt.Errorf("error creating gzip reader for %v: %v",
				self.RemoteFile, err)
		}

		tarReader := tar.NewReader(gzipReader)
		err = archive.Extract(tarReader, self.ExtractTo)
		if err != nil {
			return fmt.Errorf("error extracting %v to %v: %v", self.RemoteFile,
				self.ExtractTo, err)
		}
	}

	return nil
}