[英]Upload files folder and subfolder in s3 using boto3 python
Hi Team I am trying to upload a react build folder on AWS s3 using a python script, I am able to do so using the below script, but I am not able to resolve the path on S3.嗨团队我正在尝试使用 python 脚本在 AWS s3 上上传一个反应构建文件夹,我可以使用以下脚本执行此操作,但我无法解析 S3 上的路径。
Like the path of the static folder should be static/css/main.e412e58a.css static/css/main.e412e58a.css.map Like the path of the static folder should be static/css/main.e412e58a.css static/css/main.e412e58a.css.map
Please suggest to me where I am lacking.请向我建议我缺乏的地方。
my code is as:-我的代码是: -
#!/usr/bin/env python
import boto3
from botocore.exceptions import ClientError
import sys
import os
import logging
from pathlib import Path
bucket = 'post.com'
src_dir = 'C:/Users/radhey/Desktop/PostCodes/PostCodesUI/build'
def upload_file(file_name, bucket, object_name=None, ExtraArgs={'ContentType':'text/html'}):
"""Upload a file to an S3 bucket
:param file_name: File to upload
:param bucket: Bucket to upload to
:param object_name: S3 object name. If not specified then file_name is used
:return: True if file was uploaded, else False
"""
# If S3 object_name was not specified, use file_name
if object_name is None:
object_name = os.path.basename(file_name)
# Upload the file
s3_client = boto3.client('s3')
try:
response = s3_client.upload_file(file_name, bucket, object_name, ExtraArgs=ExtraArgs)
except ClientError as e:
logging.error(e)
return False
return True
if __name__ == "__main__":
for path in Path(src_dir).rglob('*'):
if path.is_file():
filename = path.relative_to(src_dir)
filename = str(filename)
path = str(path)
if filename.endswith('html'):
ExtraArgs = {'ContentType': 'text/html'}
elif filename.endswith('css'):
ExtraArgs = {'ContentType': 'text/css'}
elif filename.endswith('json'):
ExtraArgs = {'ContentType': 'application/json'}
elif filename.endswith('png'):
ExtraArgs = {'ContentType': 'image/png'}
elif filename.endswith('jpeg') or filename.endswith('jpg'):
ExtraArgs = {'ContentType': 'image/jpeg'}
upload_file(path, bucket, filename, ExtraArgs=ExtraArgs)
For S3 object keys, backslash \
is considered a "character to avoid".对于 S3 object 键,反斜杠
\
被视为“要避免的字符”。 If you replace the backslashes with forward slashes /
, S3 should be able to interpret them as the delimiters to a folder structure for your React application.如果将反斜杠替换为正斜杠
/
,S3 应该能够将它们解释为 React 应用程序文件夹结构的分隔符。
object_name.replace('\', '/')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.