簡體   English   中英

獲取s3文件夾中所有s3文件的Object URL

[英]Get Object URL of s3 all s3 files in a folder

使用pythonaws cli獲取 object url s3 存儲桶文件夾中的所有文件。
S3 object url 是格式
https://BUCKET_NAME.s3.amazonaws.com/FOLDER_1/FILE_NAME

您可以使用腳本生成 S3 文件夾中所有文件的 Object Url

import boto3

s3_uri="s3://BUCKET_NAME/FOLDER_1/FOLDER2/" # S3 URI of the folder you want to recursively scan, Replace this with your own S3 URI

# Split the s3 uri to extract bucket name and the file prefix
# Splitting S3 URI will generate an array
# Combine the appropirate elements of the array to extraxt BUCKET_NAME and PREFIX
arr=s3_uri.split('/')
bucket =arr[2]
prefix=""
for i in range(3,len(arr)-1):
    prefix=prefix+arr[i]+"/"
    
s3_client = boto3.client("s3")

def list_s3_files_using_client(bucket,prefix,s3_client):
    response = s3_client.list_objects_v2(Bucket=bucket,  Prefix=prefix) # Featch Meta-data of all the files in the folder
    files = response.get("Contents")
    for file in files: # Iterate through each files
        file_path=file['Key']
        object_url="https://"+bucket+".s3.amazonaws.com/"+file_path #create Object URL  Manually
        print("Object Url =  "+object_url)

list_s3_files_using_client(bucket=bucket,prefix=prefix,s3_client=s3_client)

Output

https://BUCKET_NAME.amazonaws.com/FOLDER_1/FOLDER_2/FILENAME_1
https://BUCKET_NAME.amazonaws.com/FOLDER_1/FOLDER_2/FILENAME_2
https://BUCKET_NAME.amazonaws.com/FOLDER_1/FOLDER_2/FILENAME_3
https://BUCKET_NAME.amazonaws.com/FOLDER_1/FOLDER_2/FILENAME_4
.
.
.
https://BUCKET_NAME.amazonaws.com/FOLDER_1/FOLDER_2/FILENAME_N

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM