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.
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". 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.
object_name.replace('\', '/')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.