Boto3 list items of s3 bucket and give path
I need to list all the items in my media/uploads/ folder on my s3 bucket. I've tried several answers from similar questions but was unable to implement the code snippets. I'm confused how to set up the boto3 connection and so far did not get a list. I work with django. In the end I need a list of the latest items. But getting a list of all the items in that folder would already help a lot.
Here is my storage system:
settings.py
STATIC_URL = "static/"
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
AWS_STORAGE_BUCKET_NAME = "feedingcycle"
AWS_S3_REGION_NAME = "eu-central-1"
AWS_ACCESS_KEY_ID = "xxxx"
AWS_SECRET_ACCESS_KEY = "xxxx"
AWS_S3_CUSTOM_DOMAIN = f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com"
import boto3
bucket_name = AWS_STORAGE_BUCKET_NAME
def keys(bucket_name, prefix='/', delimiter='/'):
prefix = prefix.lstrip(delimiter)
bucket = boto3.resource('s3').Bucket(bucket_name)
return (_.key for _ in bucket.objects.filter(Prefix=prefix)), print(keys)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILES_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
MEDIAFILES_FOLDER = "media"
DEFAULT_FILE_STORAGE = "custom_storages.MediaFileStorage"
The def keys is not working in there. I also don't get any error messages... Can I set up the whole thing also in my settings or do I have to make a new .py or model for that?
Best wishes, Amber