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);
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)
}