【发布时间】:2020-12-01 15:43:08
【问题描述】:
我正在将大量文件从一个 S3 帐户转移到另一个帐户,但其中一些文件出现了一些问题。
在 S3 中,我有一些文件名,末尾带有一些奇怪的字符。心或 100 的小图片!管他呢。像这样:
在看了太久之后,我决定像这样的键有问题。 S3 中的实际 Object url 看起来像
https://infoYouDoNotNeed+Resume%F0%9F%92%9B%F0%9F%92%9A%E2%9D%A4.docx
但是如果我打印我的 copy_source,键看起来像
'Key': u'notNeededInfo\U0001f49b\U0001f49a\u2764.docx'
当我运行脚本时,我在终端中看到以下错误
An error occurred (InvalidRequest) when calling the CopyObject operation: Couldn't parse the specified URI.
我 99% 确定这与那个密钥有关,但我无法弄清楚如何修复它。我尝试过使用 urllib、编码、解码、替换,但似乎没有任何东西可以将其发送到我的 s3.meta.client.copy 函数中将复制的地址。
以下是相关代码:
def get_versions(bucket_object_key):
# get a list of versions for the object
response = source_client.list_object_versions(
Bucket=args.source,
Prefix=bucket_object_key
)
# this needs to go in reverse order
# for each version in order from first entered to most recent...
for version in response['Versions'][::-1]:
# get the key
v_key = version['Key']
# encode v_key to ignore out of bounds ascii characters
new_key = version['Key'].encode('ascii', 'ignore')
# get the versionId
v_id = version['VersionId']
try:
# set the arguments for the source, filename, and version
copy_source = {
'Bucket': args.source,
'Key': v_key,
'VersionId': v_id
}
print copy_source
# copy the file/version to the destination bucket
s3.meta.client.copy(
copy_source,
dest_bucket,
new_key
)
# Log the success
logging.info('File %s copied, version %s' % (v_key, v_id))
dest_object = dest_client.get_object(
Bucket=args.dest,
Key=new_key
)
new_v_id = dest_object['VersionId']
logging.info('%s New versionId %s' % (v_key, new_v_id))
print('copy of %s version %s successful' % (v_key, v_id))
except ClientError as e:
# log the failure
logging.error('Error copying %s, version %s' % (key, v_id))
print 'Error copying {} {}'.format(
key.encode('ascii', 'ignore'),
v_id
)
print e
我很想就如何使这项工作提出一些建议,因为有很多精神病患者的申请人(我假设他们对我这样做)在他们的简历中都有这些。可能是招聘人员做的,但不管怎样,有人把这些照片放在那里,我需要把这些文件移到其他地方。
【问题讨论】:
标签: python-2.7 amazon-s3 boto3