简体   繁体   中英

Upload json File to Specific Folder in s3 Bucket in Python

I have python code that uploads a json file into an s3 bucket in AWS. Below is the code that works:

ACCESS_KEY = '##########'
SECRET_KEY = '######################/####'


def upload_to_aws(schoolID, bucketName):
    s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
                      aws_secret_access_key=SECRET_KEY)

    try:
        s3.upload_file(schoolID, bucketName, schoolID)
        print(schoolID + " Upload Successful")
        return True
    except FileNotFoundError:
        print(schoolID + " The file was not found")
        return False
    except NoCredentialsError:
        print(schoolID + " Credentials not available")
        return False


uploaded = upload_to_aws(schoolID + '.json', 'bucketName')

However, I now want to upload it to a specific folder in the s3 bucket. I tried editing the following line in the above code:

uploaded = upload_to_aws(schoolID + '.json', 'bucketName\AcademicYears')

But I got this error:

ParamValidationError: Parameter validation failed:
Invalid bucket name "bucketName\AcademicYears": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$" or be an ARN matching the regex "^arn:(aws).*:s3:[a-z\-0-9]+:[0-9]{12}:accesspoint[/:][a-zA-Z0-9\-]{1,63}$"

Does anyone know how I can alter this code to make it work? Thanks in advance

It looks like you're adding a string to your bucket which AWS is rejecting. Subdirs are added to the bucket key.

If your bucket is named "bucketName" then this is the top directory. Anything under this directory will be added to the key.

For example:

  • bucketName <-- bucket

    • AcademicYears <-- subdir

      • school_id_file.json <--file

Try:

uploaded = upload_to_aws('AcademicYears/' + schoolID + '.json', 'bucketName')

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM