I am sending an excel file as a request in postman and need to upload this to s3 . I access the file from request and send it to s3.
@api_view(['POST'])
def excel_upload(request):
print("request", request)
excel_file = request.FILES['file']
print("excel_file", excel_file) // this prints the name of the excel file i am sending in request
upload_to_aws(excel_file,'X1excelsheets','s3_file_name')
and here is the function to upload file to s3.
def upload_to_aws(local_file, bucket, s3_file):
s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY)
try:
s3.upload_file(local_file, bucket, s3_file)
print("Upload Successful")
return True
except FileNotFoundError:
print("The file was not found")
return False
except NoCredentialsError:
print("Credentials not available")
return False
uploaded = upload_to_aws('local_file', 'bucket_name', 's3_file_name')
I am trying to use this particular post
https://medium.com/bilesanmiahmad/how-to-upload-a-file-to-amazon-s3-in-python-68757a1867c6
to get things done . Error:ValueError: Filename must be a string
excel_file = request.FILES['file']. Probably you should save it, and send to aws by "filename". Soexcel_file='/path/to/file/file.xls'