Skip to content

Commit

Permalink
use boto3 to access bucket regions using V4 s3 auth
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff authored and cslzchen committed Aug 8, 2023
1 parent 3a63724 commit cb47a9e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
1 change: 1 addition & 0 deletions addons/s3/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
boto==2.38.0
boto3==1.4.7
28 changes: 14 additions & 14 deletions addons/s3/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from rest_framework import status as http_status

import boto3
from boto import exception
from boto.s3.connection import S3Connection
from boto.s3.connection import OrdinaryCallingFormat
Expand Down Expand Up @@ -131,26 +132,25 @@ def get_bucket_location_or_error(access_key, secret_key, bucket_name):


def get_bucket_prefixes(access_key, secret_key, prefix, bucket_name):
connection = S3Connection(access_key, secret_key)

if bucket_name != bucket_name.lower() or '.' in bucket_name:
# Must use ordinary calling format for mIxEdCaSe or `.` containing bucket names
# otherwise use the default as it handles bucket outside of the US
connection.calling_format = OrdinaryCallingFormat()

bucket = connection.get_bucket(bucket_name)
s3 = boto3.client(
's3',
aws_access_key_id=access_key,
aws_secret_access_key=secret_key
)

result = s3.list_objects(Bucket=bucket_name, Prefix=prefix, Delimiter='/')
folders = []
for key in bucket.list(delimiter='/', prefix=prefix):
if key.name.endswith('/') and key.name != prefix:
for common_prefixes in result.get('CommonPrefixes', []):
key_name = common_prefixes.get('Prefix')
if key_name != prefix:
folders.append(
{
'path': key.name,
'id': f'{bucket_name}:/{key.name}',
'folder_id': key.name,
'path': key_name,
'id': f'{bucket_name}:/{key_name}',
'folder_id': key_name,
'kind': 'folder',
'bucket_name': bucket_name,
'name': key.name.split('/')[-2],
'name': key_name.split('/')[-2],
'addon': 's3',
}
)
Expand Down

0 comments on commit cb47a9e

Please sign in to comment.