【问题标题】:How to delete S3 object in AWS-SDK-Go?如何删除 AWS-SDK-Go 中的 S3 对象?
【发布时间】:2019-03-17 16:28:19
【问题描述】:

我想删除bucket/userID
但是bucket/userID下有很多文件

我必须实现删除bucket/userID,需要使用ListObjects 然后DeleteObjects。 函数ListObjects 返回result.Contents[]*s3.Object 但是DeleteObjects 需要[]*s3.ObjectIdentifier

我无法将[]*s3.Object 转换为[]*s3.ObjectIdentifier
在这段代码中,发生了错误invalid memory address or nil pointer dereference

type Object struct {
    _ struct{} `type:"structure"`

     ETag *string `type:"string"`
     Key *string `min:"1" type:"string"`
    LastModified *time.Time `type:"timestamp" 
    timestampFormat:"iso8601"`
    Owner *Owner `type:"structure"`
    Size *int64 `type:"integer"`
    StorageClass *string `type:"string" enum:"ObjectStorageClass"`
}

type ObjectIdentifier struct {
    _ struct{} `type:"structure"`

    Key *string `min:"1" type:"string" required:"true"`
    VersionId *string `type:"string"`
}

objects := getObjects() // return []*s3.Object
a := make([]*s3.ObjectIdentifier, len(objects))

for i, v := range objects {
        a[i].Key = v.Key
}

a[i].Key = v.Key 是错误的。 如何实现删除bucket/userID

【问题讨论】:

    标签: amazon-s3 aws-sdk aws-sdk-go


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      在您的实现中,a := make([]*s3.ObjectIdentifier, len(objects)) 仅声明这些变量。它不会为每个结构初始化数组。结果,它会创建一个零指针异常。

      您需要在迭代中初始化所有结构:

      ...
      for i, v := range objects {
              a[i] = &s3.ObjectIdentifier{
                      Key: v.Key,  
              }
      }
      

      构造[]*s3.ObjectIdentifier后,可以根据AWS Golang的文档调用DeleteObjects参数DeleteObjectsInput

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-27
        • 2017-03-06
        • 1970-01-01
        • 2012-08-10
        • 2016-05-10
        • 2015-03-01
        • 2020-06-14
        • 1970-01-01
        相关资源
        最近更新 更多