Search code examples
pythonamazon-web-servicesamazon-s3amazon-transcribe

How to get uri path for a file stored in s3 Bucket


My Bucket name 'ABC' has a structure as follows:

audiofiles
       audio_one.wav
       audio_two.mp3

I want a python code to get the URI of these files not the file or file list, the file uri so that I can use the file as the input link in the transcribe job.


Solution

  • The boto3 s3 client does not have a method to return the keys/files URLs.

    But we can use the Public URL of each object:

    import boto3
    
    bucket_name = 'bucket_name'
    s3 = boto3.resource('s3')
    
    
    def lambda_handler(event, context):
        my_bucket = s3.Bucket(bucket_name)
    
        for obj in my_bucket.objects.all():
            url = f'https://{bucket_name}.s3.amazonaws.com/{obj.key}'
            print(url)
    

    This will print something like this for you:

    https://bucket_name.s3.amazonaws.com/audio_one.wav
    https://bucket_name.s3.amazonaws.com/audio_two.mp3
    https://bucket_name.s3.amazonaws.com/audio_three.mp3