2017-12-03 10 views
-3

golang sdkを使用してAWS S3にオブジェクトをアップロードしようとしています(文字列のみをアップロードしようとしています)。しかし、私はそれを達成するのが難しいです。 AWS S3にファイルを作成せずにアップロードするにはどうすればよいのか、誰かに教えていただけますか?aws-sdk-goを使用してファイルを作成せずにAWS S3にオブジェクトをアップロード

ファイルアップロードする方法のAWS例:

// Creates a S3 Bucket in the region configured in the shared config 
// or AWS_REGION environment variable. 
// 
// Usage: 
// go run s3_upload_object.go BUCKET_NAME FILENAME 
func main() { 
    if len(os.Args) != 3 { 
     exitErrorf("bucket and file name required\nUsage: %s bucket_name filename", 
      os.Args[0]) 
    } 

    bucket := os.Args[1] 
    filename := os.Args[2] 

    file, err := os.Open(filename) 
    if err != nil { 
     exitErrorf("Unable to open file %q, %v", err) 
    } 

    defer file.Close() 

    // Initialize a session in us-west-2 that the SDK will use to load 
    // credentials from the shared credentials file ~/.aws/credentials. 
    sess, err := session.NewSession(&aws.Config{ 
     Region: aws.String("us-west-2")}, 
    ) 

    // Setup the S3 Upload Manager. Also see the SDK doc for the Upload Manager 
    // for more information on configuring part size, and concurrency. 
    // 
    // http://docs.aws.amazon.com/sdk-for-go/api/service/s3/s3manager/#NewUploader 
    uploader := s3manager.NewUploader(sess) 

    // Upload the file's body to S3 bucket as an object with the key being the 
    // same as the filename. 
    _, err = uploader.Upload(&s3manager.UploadInput{ 
     Bucket: aws.String(bucket), 

     // Can also use the `filepath` standard library package to modify the 
     // filename as need for an S3 object key. Such as turning absolute path 
     // to a relative path. 
     Key: aws.String(filename), 

     // The file to be uploaded. io.ReadSeeker is preferred as the Uploader 
     // will be able to optimize memory when uploading large content. io.Reader 
     // is supported, but will require buffering of the reader's bytes for 
     // each part. 
     Body: file, 
    }) 
    if err != nil { 
     // Print the error and exit. 
     exitErrorf("Unable to upload %q to %q, %v", filename, bucket, err) 
    } 

    fmt.Printf("Successfully uploaded %q to %q\n", filename, bucket) 
} 

を私はすでに、プログラムファイルを作成しようとしましたが、それは私のシステム上のファイルを作成し、S3にアップロードされます。

+1

あなたがすでにやろうとしたことを投稿してください。 – peteb

+0

AWSの例が示唆していることを試しただけです。あなたのシステムからファイルを開き、それをS3にアップロードします。私は例を使って質問を編集するつもりです。私の悪い。 –

+2

アップロードしようとしているものを表示します。 s3manager.UploadInput.Bodyはio.Readerです。 bytes.NewReader、strings.NewReader、bytes.Buffer、またはインターフェイスをサポートする他の数の型を使用してio.Readerを作成します。 –

答えて

1

UploadInput構造のBodyフィールドは、ちょうどio.Readerです。したがって、あなたが欲しいものをio.Readerに渡します。ファイルである必要はありません。

+0

ええ、それはまったく私の悪い私はそれがio.Readerであることに気付かなかった。私の悪い。 –

関連する問題