本文整理匯總了Golang中github.com/AdRoll/goamz/s3.Bucket.PutReader方法的典型用法代碼示例。如果您正苦於以下問題:Golang Bucket.PutReader方法的具體用法?Golang Bucket.PutReader怎麽用?Golang Bucket.PutReader使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/AdRoll/goamz/s3.Bucket
的用法示例。
在下文中一共展示了Bucket.PutReader方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: makeupload
// Generate a function to use for uploading files as we walk the file tree from
// `base`.
func makeupload(base string, pattern *regexp.Regexp, bucket *s3.Bucket, bucketPrefix string, dryRun bool, progress *Progress) func(string, os.FileInfo, error) error {
// Remove any excess path separators from the bucket prefix.
bucketPrefix = strings.Trim(bucketPrefix, "/")
// Get a canonical version of the base dir
cleanBase := filepath.Clean(base)
// Create a closure for the upload function.
return func(path string, fi os.FileInfo, err error) (errOut error) {
if err != nil {
return err
}
if fi.IsDir() {
return nil
}
//fmt.Printf("Found an item: %s\n", path)
baseName := filepath.Base(path)
if !pattern.MatchString(baseName) {
//fmt.Printf("Item does not match pattern\n")
return nil
}
// Make sure we're comparing apples to apples when stripping off the
// base path. Use the canonical versions of both.
cleanPath := filepath.Clean(path)
relPath := cleanPath[len(cleanBase)+1:]
// If we encounter a file
// /path/to/base/foo/bar/baz
// <--- base ---><-- rel -->
// and our bucket prefix is `hello/files`, our file in S3 will be at
// s3://bucket-name/hello/files/foo/bar/baz
// <- prefix -><-- rel -->
s3Path := fmt.Sprintf("%s/%s", bucketPrefix, relPath)
// Update progress Count whether we were successful or not.
progress.Count += 1
if dryRun {
fmt.Printf("Dry run. Not uploading item to %s\n", s3Path)
return
}
fmt.Printf("Uploading item to: %s\n", s3Path)
reader, err := os.Open(path)
if err != nil {
fmt.Printf("Error opening %s for reading: %s\n", path, err)
progress.Errors++
return err
}
err = bucket.PutReader(s3Path, reader, fi.Size(), "binary/octet-stream", s3.BucketOwnerFull, s3.Options{})
if err != nil {
progress.Errors++
return err
}
// Count the bytes for this file as progress if there were
// no upoad errors.
progress.Bytes += fi.Size()
err = reader.Close()
if err != nil {
fmt.Printf("Error closing %s: %s\n", path, err)
progress.Errors++
errOut = err
}
// Now remove the file locally.
err = os.Remove(path)
if err != nil {
fmt.Printf("Error removing %s: %s\n", path, err)
progress.Errors++
errOut = err
}
return
}
}