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

Time:01-20

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.

CodePudding user response:

The boto3 s3 client do 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://my-bucket.s3.amazonaws.com/audio_one.wav
https://my-bucket.s3.amazonaws.com/audio_two.mp3
https://my-bucket.s3.amazonaws.com/audio_three.mp3
  •  Tags:  
  • Related