在Golang中写入/追加到S3存储桶

huangapple go评论130阅读模式
英文:

Write/Append to S3 bucket in Golang

问题

我正在工作的应用程序目前使用Amazon S3 Stream Wrapper for PHP来将日志消息写入S3存储桶,我需要将其迁移到Golang。Go语言中是否有类似的功能?PutObject方法会覆盖所有内容,而我想要追加内容。在PHP中,我们使用fopen()fwrite()将字符串追加到现有的存储桶中。我希望我可以这样做:

os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0600)

但是我收到一个文件不存在的错误。我尝试使用s3://https://链接到日志文件。

以下是PHP的Amazon示例:

$stream = fopen('s3://bucket/key', 'w');
fwrite($stream, 'Hello!');
fclose($stream);
英文:

The application I'm working on currently uses the Amazon S3 Stream Wrapper for PHP in order to write log messages to an S3 bucket and I need to port it over to Golang. Is there an equivilent to this in Go? The PutObject method overwrites all the contents and I want to just append. In PHP we are using fopen() and fwrite() to append a string to an existing bucket. I was hoping I could just do

os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0600) 

but I get an error saying the file doesn't exist. I tried using both s3:// and the https:// link to the log file.

Here's the Amazon example for PHP:

$stream = fopen('s3://bucket/key', 'w');
fwrite($stream, 'Hello!');
fclose($stream);

答案1

得分: 2

如@Rob所提到的,S3不支持追加操作https://forums.aws.amazon.com/message.jspa?messageID=540395。

唯一的方法是下载对象并使用aws-sdk或minio(https://github.com/minio/minio)上传新对象。我猜,PHP包装器在底层执行相同的操作。

使用minio的示例代码:

s3Client, err := minio.New("s3.amazonaws.com", "YOUR-ACCESS-KEY-HERE", "YOUR-SECRET-KEY-HERE", true)
if err != nil {
log.Fatalln(err)
}

reader, err := s3Client.GetObject("my-bucketname", "my-objectname")
if err != nil {
log.Fatalln(err)
}
defer reader.Close()

// 修改你的数据

n, err := s3Client.PutObject("my-bucketname", "my-objectname", reader, "application/octet-stream")
if err != nil {
log.Fatalln(err)
}

英文:

As @Rob mentioned, S3 doesn't support append operation https://forums.aws.amazon.com/message.jspa?messageID=540395.

The only way is download object and upload the new one using aws-sdk or minio (https://github.com/minio/minio). I suppose, PHP wrapper make the same under the hood.

The sample with minio:

s3Client, err := minio.New("s3.amazonaws.com", "YOUR-ACCESS-KEY-HERE", "YOUR-SECRET-KEY-HERE", true)
if err != nil {
	log.Fatalln(err)
}

reader, err := s3Client.GetObject("my-bucketname", "my-objectname")
if err != nil {
	log.Fatalln(err)
}
defer reader.Close()

// Change your data

n, err := s3Client.PutObject("my-bucketname", "my-objectname", reader, "application/octet-stream")
if err != nil {
	log.Fatalln(err)
}

huangapple
  • 本文由 发表于 2017年3月9日 04:20:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/42681181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定