duanpu1111 2018-08-24 20:38
浏览 165
已采纳

如何将S3数据加载到本地内存中,而不是保存到文件中

This is my current code:

import (
  "time"
  "reflect"
  "io/ioutil"
  "fmt"
  "os"
  "github.com/golang/protobuf/proto"
  "github.com/aws/aws-sdk-go/service/s3"
  "github.com/aws/aws-sdk-go/service/s3/s3manager"
  "github.com/aws/aws-sdk-go/aws"
  "github.com/aws/aws-sdk-go/aws/session"
  "github.com/aws/aws-sdk-go/aws/credentials"
)

f, err := os.Create(filename)
if err != nil {
  panic(fmt.Errorf("failed to create file %q, %v", filename, err))
}
_, err = db.Downloader.Download(f, &s3.GetObjectInput{
    Bucket: aws.String("bucketName"),
    Key:    aws.String("myKeyThatMightExist"),
})
if err != nil { // key doesn't exist
  os.Remove(filename)
  return nil
} else {
  file, err = ioutil.ReadFile(filename)
}

I need file to be a string that is what was pulled down from S3. The local file that I am storing is a cache for what is on S3.

This means that when I try to pull down data, I need to create a file for that data. Due to the database I'm working with, I don't actually know if the key I'm accessing even exists. I was wondering if either of these solutions would be possible: 1) Create some type of buffer object, to load the whole file into RAM, without actually saving it. 2) Check if the file exists on S3, without creating a local file. This would let me then pull down the file if it existed, while not creating an empty file if I didnt exist on S3.

Sorry, but I can't create a playground link, as go playground does not have the libraries I need for this example.

Thanks, Neil

</div>
  • 写回答

1条回答 默认 最新

  • douhui1630 2018-08-25 08:53
    关注
    buf := aws.NewWriteAtBuffer([]byte{})
    downloader.Download(buf, &requestInput)
    

    Now you can use buf.Bytes()

    https://docs.aws.amazon.com/sdk-for-go/api/aws/#NewWriteAtBuffer

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b