2

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

我必须执行删除bucket/userID,需要使用ListObjectsthen DeleteObjects。函数ListObjects返回result.Contents的是[]*s3.Object But DeleteObjectsneeds []*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

4

2 回答 2

0

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

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

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

构建好之后,就可以根据 AWS Golang 的文档[]*s3.ObjectIdentifier调用参数DeleteObjects了。DeleteObjectsInput

于 2019-03-17T08:28:19.240 回答
0

Go 开发人员指南有一个主题和代码,关于删除存储桶中的所有对象:https ://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/s3-example-basic-bucket -operations.html#s3-examples-bucket-ops-delete-all-bucket-items

于 2018-02-20T16:32:53.327 回答