首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在AWS-SDK-Go中删除S3对象?

如何在AWS-SDK-Go中删除S3对象?
EN

Stack Overflow用户
提问于 2018-02-02 20:21:48
回答 2查看 5.1K关注 0票数 2

我想删除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

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

发布于 2018-02-21 00:32:53

票数 0
EN

Stack Overflow用户

发布于 2019-03-17 16:28:19

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

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

代码语言:javascript
复制
...
for i, v := range objects {
        a[i] = &s3.ObjectIdentifier{
                Key: v.Key,  
        }
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48582596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档