此代码从s3获取所有对象并删除对象
从getAllObjects
调用DeletePhotosFromS3
。
我在objects
中的DeletePhotosFromS3
中获得2个不同的密钥
但是deleteObjects
有两个相同的密钥。前[{Key: 1}, {Key: 1}]
。
为什么deleteObjects
有两个相同的密钥以及如何在objects
中设置[]*s3.ObjectIdentifier
?
func getAllObject(userID string) (*[]string, error) {
var objects []string
svc := initS3()
config := model.NewConfig()
input := &s3.ListObjectsInput{
Bucket: aws.String(config.AWSS3Bucket),
Prefix: aws.String(userID),
MaxKeys: aws.Int64(2), // default 1000
}
result, err := svc.ListObjects(input)
if err != nil {
return &objects, err
}
for _, v := range result.Contents {
objects = append(objects, *v.Key)
}
return &objects, nil
}
func DeletePhotosFromS3(userID string) (error) {
var deleteObjects []*s3.ObjectIdentifier
svc := initS3()
config := model.NewConfig()
objects, err := getAllObject(userID) // called getAllObject
for _, v := range *objects {
deleteObjects = append(deleteObjects, &s3.ObjectIdentifier{Key: &v}) // Er
}
...
}
答案 0 :(得分:2)
for循环中的迭代值v
将在每次迭代时重用。指针&v
对于附加到列表的每个项目都是相同的。修正了片段:
for _, v := range *objects {
vcopy := v
deleteObjects = append(deleteObjects, &s3.ObjectIdentifier{Key: &vcopy})
}