首页 > 解决方案 > 尝试将已散列到 AWS S3 存储桶的文件名上传

问题描述

我正在尝试将文件上传到 AWS S3,文件密钥是文件创建日期与文件本身的 MD5 散列连接。我生成哈希没有问题,但是在尝试上传文件时出现以下错误。

botocore.exceptions.ClientError: An error occurred (InvalidURI) when calling the PutObject operation: Couldn't parse the specified URI.

我要上传的文件是下载的网页。

这是我的代码

#new_page_content holds the downloaded html file I'm trying to upload.
new_page_content_encode("UTF-8") 
current_time = time.strftime("%Y_%m_%d_%H_%M_%S_", time.gmtime())
md5_hash_func = hashlib.md5()
md5_hash_func.update(new_page_content.encode("UTF-8"))
hashed_string = md5_hash_func.digest()
key_string = current_time + hashed_string

os.remove(LOCAL_PATH)
new_local_file = io.open(LOCAL_PATH, mode="w", encoding="utf-8")
new_local_file.write(new_page_content)
new_local_file.close()

#Uploading starts here
s3 = boto3.client(
        's3',
        aws_access_key_id=ACCESS_KEY,
        aws_secret_access_key=SECRET_KEY,
    )
data = open(LOCAL_PATH, "rb")
s3.upload_fileobj(data, BUCKET, key_string)
data.close()

我很确定问题是我使用了文档定义的不安全字母。我想知道在上传文件时是否有任何能力绕过它?

我已经看到有关下载文件的类似问题,但我不太清楚如何将解决方案转换为上传。

标签: pythonamazon-web-servicesamazon-s3

解决方案


解决了!

我用hexdigest而不是digest得到了安全的字符。


推荐阅读