我想删除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
发布于 2018-02-21 00:32:53
发布于 2019-03-17 16:28:19
在您的实现中,a := make([]*s3.ObjectIdentifier, len(objects))仅声明这些变量。它不会为每个结构初始化数组。因此,它将创建一个空指针异常。
您需要初始化迭代中的所有结构:
...
for i, v := range objects {
a[i] = &s3.ObjectIdentifier{
Key: v.Key,
}
}在构建[]*s3.ObjectIdentifier之后,您可以根据AWS Golang的文档使用DeleteObjectsInput参数调用DeleteObjects。
https://stackoverflow.com/questions/48582596
复制相似问题