【问题标题】:File uploaded to s3 but it is not visible文件上传到 s3 但不可见
【发布时间】:2018-03-14 18:56:32
【问题描述】:

我正在尝试使用 GoLang 将图像上传到 s3 存储桶。 我已经提到了程序的功能体。它将图像文件作为多部分表单数据和我应该将图像保存在 s3 中的文件名。

func uploadImage(res http.ResponseWriter, req *http.Request) {
file, handler, err := req.FormFile("uploaded_file") // uploaded_file is the key(input field name) of the form-data
if err != nil {
    fmt.Println(err)
    return
}

type Resp struct {
    Status   string `json:"status"`
    Filename string `json:"filename"`
}
filename := req.Form.Get("filename")

rnd := render.New()
var Response Resp
Response.Status = "failed"
if filename != "" {
    // define credentials
    awsAccessKey := "KEY"
    awsSecret := "SECRET"
    token := ""
    // set credentials
    creds := credentials.NewStaticCredentials(awsAccessKey, awsSecret, token)
    credentials, err := creds.Get()
    if err != nil {
        fmt.Printf("bad credentials: %s", err)
    }
    fmt.Println(credentials)
    //setup config and s3 settings
    cfg := aws.NewConfig().WithRegion("ap-southeast-1").WithCredentials(creds)

    //create s3 credentail
    svc := s3.New(session.New(), cfg)
    type Sizer interface {
        Size() int64
    }

    size := file.(Sizer).Size()
    // create a buffer of size = images size
    buffer := make([]byte, size)
    file.Read(buffer)
    fileBytes := bytes.NewReader(buffer)
    fileType := http.DetectContentType(buffer)

    path := "mybucketname/" + filename
    params := &s3.PutObjectInput{
        Bucket:        aws.String("mybucketname"),
        Key:           aws.String(path),
        Body:          fileBytes,
        ContentLength: aws.Int64(size),
        ContentType:   aws.String(fileType),
        ACL:           aws.String("public-read"),
    }
    resp, err := svc.PutObject(params)
    if err != nil {
        fmt.Printf("bad response: %s", err)
    }
    fmt.Printf("response %s", awsutil.StringValue(resp))

    Response.Status = "success"
    Response.Filename = awsutil.StringValue(resp)

}

defer file.Close()
res.Header().Set("Content-Type", "application/json")
rnd.JSON(res, http.StatusOK, Response)
}

此函数在成功上传图片时返回状态为成功,并将文件名返回为http响应。

在控制台中,我可以看到一些响应:

response {
ETag: "\"bd9be6ada974e66a154552edc78b4a81\""
}

我不知道 ETag 是什么意思,但我知道图片已上传,并且我得到了 s3 的一些积极响应。但我无法找到我在 s3 中上传的图片。

我需要一些帮助来解决这个问题。 提前致谢。

【问题讨论】:

  • 接下来,我将使用类似于of this answer 的方法来 1) 枚举您帐户中的存储桶并验证“mybucketname”是否存在,如果成功,则 2) 列出其中的内容桶。

标签: go amazon-s3 image-uploading multipartform-data


【解决方案1】:

您可以像这样检索上传的 s3 对象:

svc := s3.New(session.New(), cfg)

path := "mybucketname/" + filename
input := &s3.GetObjectInput{
    Bucket: aws.String("mybucketname"),
    Key:    aws.String(path),
}

result, err := svc.GetObject(input)
if err != nil {
    if aerr, ok := err.(awserr.Error); ok {
        switch aerr.Code() {
        case s3.ErrCodeNoSuchKey:
            fmt.Println(s3.ErrCodeNoSuchKey, aerr.Error())
        default:
            fmt.Println(aerr.Error())
        }
    } else {
        // Print the error, cast err to awserr.Error to get the Code and
        // Message from an error.
        fmt.Println(err.Error())
    }
    return
}

fmt.Println(result)

无耻插件: 您也可以使用我从以下链接创建的命令行工具来查看对象。

https://github.com/bharath-srinivas/nephele

你只需要这样做:

$ nephele s3 list <bucket-name> --prefix <prefix of the key/filename>

您可以在这里参考aws-sdk-go的官方文档:https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#S3.GetObject

【讨论】:

    【解决方案2】:

    检查结果。位置。

    从文档@https://docs.aws.amazon.com/sdk-for-go/api/service/s3/开始,逐行修改,直到得到你想要的。

    【讨论】:

    • 你能告诉我如何处理那个 ETag 吗? @ZAky
    猜你喜欢
    • 2019-05-03
    • 2020-07-22
    • 2022-06-11
    • 2017-11-14
    • 2019-10-13
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多