The Body
field of the UploadInput
struct is just an io.Reader
. So pass any io.Reader
you want--it doesn't need to be a file.
将对象上传到AWS S3,而无需使用aws-sdk-go创建文件
I am trying to upload an object to AWS S3 using golang sdk without needing to create a file in my system (trying to upload only the string). But I am having difficulties to accomplish that. Can anybody give me an example of how can I upload to AWS S3 without needing to create a file?
AWS Example of how to upload a file:
// 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
Usage: %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
", filename, bucket)
}
I already tried to create the file programmatically but it is creating the file on my system and then uploading it to S3.
- 点赞
- 写回答
- 关注问题
- 收藏
- 复制链接分享
- 邀请回答
3条回答
为你推荐
- Snowball Edge-Golang中的aws-sdk-go软件包-无法连接到S3
- IT行业问题
- 计算机技术
- it技术
- 编程语言问答
- 互联网问答
- 1个回答
- Golang AWS S3批处理对象创建
- IT行业问题
- 计算机技术
- it技术
- 编程语言问答
- 互联网问答
- 1个回答
- 使用S3 Golang SDK从S3下载选择性文件
- IT行业问题
- 计算机技术
- it技术
- 编程语言问答
- 互联网问答
- 1个回答
- 将文件放置到Golang中的Amazon S3后,如何获取文件URL?
- file-upload
- 2个回答
- 在AWS-SDK-GO中使用正则表达式过滤AWS资源
- IT行业问题
- 计算机技术
- it技术
- 编程语言问答
- 互联网问答
- 1个回答
- 在AWS Go-SDK中从VPC中的Lambda访问s3
- IT行业问题
- 计算机技术
- it技术
- 编程语言问答
- 互联网问答
- 2个回答
- 使用s3manager golang上传文件
- IT行业问题
- 计算机技术
- it技术
- 编程语言问答
- 互联网问答
- 3个回答
- 使用Golang的AWS S3并行下载
- IT行业问题
- 计算机技术
- it技术
- 编程语言问答
- 互联网问答
- 1个回答
- AWS S3上传的文件未显示
- IT行业问题
- 计算机技术
- it技术
- 编程语言问答
- 互联网问答
- 1个回答
- 将对象上传到AWS S3,而无需使用aws-sdk-go创建文件
- IT行业问题
- 计算机技术
- it技术
- 编程语言问答
- 互联网问答
- 3个回答
- AWS S3 Listing API-如何列出具有特定前缀的S3存储桶中的所有内容
- IT行业问题
- 计算机技术
- it技术
- 编程语言问答
- 互联网问答
- 1个回答
- 如何在Golang中从AWS S3获取资源URL
- IT行业问题
- 计算机技术
- it技术
- 编程语言问答
- 互联网问答
- 1个回答
- 使用AWS开发工具包Go的完整URI从S3下载文件
- IT行业问题
- 计算机技术
- it技术
- 编程语言问答
- 互联网问答
- 4个回答
- 有没有一种方法可以使用aws-sdk-go将数据流式传输到亚马逊s3文件,类似于Google存储Write()方法?
- IT行业问题
- 计算机技术
- it技术
- 编程语言问答
- 互联网问答
- 1个回答
- 如何在S3中保存数据流? aws-sdk-go示例不起作用?
- IT行业问题
- 计算机技术
- it技术
- 编程语言问答
- 互联网问答
- 1个回答
- 使用AWS PHP SDK上传到AWS S3无法通过身份验证
- php
- 2个回答
- 使用AWS PHP SDK列出AWS S3 Bucket中文件夹(前缀)内的所有文件
- php
- sdk
- 1个回答
- Aws Php SDk - 使用硬编码凭证创建Cloudfront分发
- php
- 1个回答
- 上传到AWS S3失败
- 使用AWS PHP SDK将文件安全传输到S3
- security
- php
- 1个回答