[英]AWS NodeJs SDK cannot delete object
I'm trying to delete an object on my S3 bucket with the aws-sdk
for Node.js using this s3.deleteObject
api:我正在尝试使用此
s3.deleteObject
api 使用 Node.js 的aws-sdk
删除我的 S3 存储桶上的 object:
async function deleteObjectAsync(bucket, key) {
try {
var extras = {
Bucket: bucket,
Key: key
};
const data = await s3
.deleteObject(extras)
.promise();
return data;
} catch (err) {
console.log(err);
}
}//deleteObjectAsync
used like像这样使用
(async (bucket, keys) => {
for (const key of keys) {
const result = await deleteObjectAsync(bucket, key);
console.log(result);
}
})(my_bucket, keys);
I get - as result - a DeleteMarker
field and a VersionId
:我得到 - 结果 - 一个
DeleteMarker
字段和一个VersionId
:
{ DeleteMarker: true, VersionId: '2wMteXstTAn6e.rsTS6wHVSerXuUMNLXw' }
but the object does not seem to be deleted on the S3 bucket, but marked for deletion.但是 object 在 S3 存储桶上似乎没有被删除,而是标记为删除。
When using the Python api:使用 Python api 时:
def delete_key(bucket, key):
deleted = True
try:
s3 = boto3.client('s3')
res = s3.delete_object(Bucket=bucket, Key=key)
except Exception as e:
print("deleted key:%s error:%s" % (key,e) )
deleted = False
pass
return deleted
the file is immediately deleted.该文件被立即删除。 Why?
为什么?
To successfully delete and object from a versioned
bucket is super simple, but the documentation isn't very clear on how
one should do it.从
versioned
存储桶中成功删除 object 非常简单,但文档并不清楚应该how
操作。
From your own example above, here follows how you could do it:从上面您自己的示例中,您可以按照以下方式进行操作:
async function deleteObjectAsync(bucket, key) {
try {
const params = {
Bucket: bucket,
Key: key,
VersionId: 'null'
};
return await s3.deleteObject(params).promise();
} catch (err) {
console.error(err);
}
}
The trick is to pass the 'null'
string as VersionId when the object doesn't have a VersionId yet.诀窍是在 object 还没有 VersionId 时将
'null'
字符串作为 VersionId 传递。 When it does then you have to delete all the previous versions and then delete the 'null'
version, which is the "original one".当它发生时,您必须删除所有以前的版本,然后删除
'null'
版本,即“原始版本”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.