Home > Software design >  How to upload a file into s3 (using Python) where the s3 bucket name has a slash '/' in th
How to upload a file into s3 (using Python) where the s3 bucket name has a slash '/' in th

Time:09-16

I have a s3 bucket (waterbucket/sample) that I am trying to write a .json file to. The s3 bucket's name however has a slash (/) in the name which causes an error to be returned when I try to move my file to that location(waterbucket/sample):

Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$" or be an ARN matching the regex "^arn:(aws).*:(s3|s3-object-lambda):[a-z\-0-9]*:[0-9]{12}:accesspoint[/:][a-zA-Z0-9\-.]{1,63}$|^arn:(aws).*:s3-outposts:[a-z\-0-9] :[0-9]{12}:outpost[/:][a-zA-Z0-9\-]{1,63}[/:]accesspoint[/:][a-zA-Z0-9\-]{1,63}$"

I was successfully able to get my .json file to write to the 'waterbucket' s3 bucket. I've googled around and have tried using a prefix (among some other things) but no luck. Is there a way I can have my python script write to the 'waterbucket/sample' bucket instead of to'waterbucket'?

Below is my python script:

import boto3
import os
import pathLib
import logging

s3 = boto3.resource("s3")
s3_client = boto3.client(service_name = 's3')
bucket_name = "waterbucket/sample"
object_name = "file.json"

file_name = os.path.join(pathlib.Path(__file__).parent.resolve(), object_name) 
s3.meta.client.upload_file(file_name, folder.format(bucket_name), object_name)

Thanks in advance!

CodePudding user response:

Bucket names can't have slashes. Thus in your case, sample must be part of the object's name, as it will be considered as s3 prefix:

bucket_name = "waterbucket"
object_name = "sample/file.json"
  • Related