python - 尝试将已散列到 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()
我很确定问题是我使用了文档定义的不安全字母。我想知道在上传文件时是否有任何能力绕过它?
我已经看到有关下载文件的类似问题,但我不太清楚如何将解决方案转换为上传。
解决方案
解决了!
我用hexdigest
而不是digest
得到了安全的字符。
推荐阅读
- reactjs - 错误:未知类型“查询”。在 assertValidSDL (GRAPHQL)
- python - Pandas DataFrame 组合多行跨列
- excel - 使用“在文本前面”参数将图像添加到 Word 页脚
- ios - 如何在swift中将一组切换设置为互斥
- r - 以响应作为因变量,截距和治疗作为自变量创建逻辑回归
- c++ - 即使我的代码没有错误,我也没有得到正确的输出
- jenkins - 如何告诉 Jenkins 构建控制台日志来混淆密码,最好没有插件?
- react-native - 如何在 React Native 中实现粘性标头?(就像在 Instagram 个人资料中一样)
- sql - 由于 SMTP 配置问题,报表服务器无法发送电子邮件
- java - 尝试在 API 测试中发布 JSON 对象时出错