app.utilities.object_storage
Object storage utilities for S3-compatible storage operations.
This module provides functions for interacting with S3-compatible object storage, including file uploads, downloads, deletions, and pre-signed URL generation.
Functions
|
Upload a file to an S3 bucket. |
|
Download a file from an S3 bucket. |
|
Delete one or more files from an S3 bucket. |
|
List all objects in an S3 bucket. |
|
Copy a file between S3 buckets or within a bucket. |
|
Configure a bucket to block all public access. |
|
Create a new S3 bucket. |
|
Generate a pre-signed URL for downloading an S3 object. |
|
Generate a pre-signed POST URL for uploading to S3. |
|
Calculate the total size of all objects in a folder. |
|
Create a file directly in S3 with the specified content. |
Generate a timestamp string for file naming. |
|
|
Generate a pre-signed upload URL for a user's profile picture. |
|
Generate a pre-signed upload URL for a batch validation file. |
|
Create a user's personal folder in object storage. |
|
Calculate the total size of a user's folder. |
Module Contents
- app.utilities.object_storage.upload_file(bucket_name, directory, file, s3, s3path=None)[source]
Upload a file to an S3 bucket.
- Parameters:
bucket_name – The name of the target S3 bucket.
directory – Local directory containing the file.
file – The filename to upload.
s3 – The boto3 S3 resource object.
s3path – Optional custom path in the bucket. Defaults to the filename.
- app.utilities.object_storage.download_file(bucket_name, directory, local_name, key_name, s3)[source]
Download a file from an S3 bucket.
- Parameters:
bucket_name – The name of the source S3 bucket.
directory – Local directory to save the file.
local_name – The local filename to save as.
key_name – The S3 object key to download.
s3 – The boto3 S3 resource object.
- app.utilities.object_storage.delete_file(bucket_name, keys, s3)[source]
Delete one or more files from an S3 bucket.
- Parameters:
bucket_name – The name of the S3 bucket.
keys – List of S3 object keys to delete.
s3 – The boto3 S3 resource object.
- app.utilities.object_storage.list_objects(bucket, s3)[source]
List all objects in an S3 bucket.
- Parameters:
bucket – The name of the S3 bucket.
s3 – The boto3 S3 resource object.
- Returns:
A list of object keys in the bucket.
- Return type:
list
- app.utilities.object_storage.copy_file(source_bucket, destination_bucket, source_key, destination_key, s3)[source]
Copy a file between S3 buckets or within a bucket.
- Parameters:
source_bucket – The name of the source bucket.
destination_bucket – The name of the destination bucket.
source_key – The S3 key of the source object.
destination_key – The S3 key for the destination object.
s3 – The boto3 S3 resource object.
- app.utilities.object_storage.prevent_public_access(bucket, s3)[source]
Configure a bucket to block all public access.
- Parameters:
bucket – The name of the S3 bucket.
s3 – The boto3 S3 resource object.
- app.utilities.object_storage.create_bucket(name, s3, secure=False)[source]
Create a new S3 bucket.
- Parameters:
name – The name for the new bucket.
s3 – The boto3 S3 resource object.
secure – If True, block all public access to the bucket.
- app.utilities.object_storage.generate_download_link(bucket_name, key, s3, expiration_in_seconds=60)[source]
Generate a pre-signed URL for downloading an S3 object.
- Parameters:
bucket_name – The name of the S3 bucket.
key – The S3 object key.
s3 – The boto3 S3 resource object.
expiration_in_seconds – URL expiration time in seconds.
- Returns:
A pre-signed URL for downloading the object.
- Return type:
str
- app.utilities.object_storage.generate_upload_link(bucket_name, file_name, file_type, s3, expiration_in_seconds=60)[source]
Generate a pre-signed POST URL for uploading to S3.
- Parameters:
bucket_name – The name of the S3 bucket.
file_name – The target filename/key in the bucket.
file_type – The Content-Type of the file being uploaded.
s3 – The boto3 S3 resource object.
expiration_in_seconds – URL expiration time in seconds.
- Returns:
JSON string containing upload data and preview URL.
- Return type:
str
- app.utilities.object_storage.folder_size(bucket_name, folder_path, s3)[source]
Calculate the total size of all objects in a folder.
- Parameters:
bucket_name – The name of the S3 bucket.
folder_path – The folder prefix to calculate size for.
s3 – The boto3 S3 resource object.
- Returns:
Total size in bytes of all objects in the folder.
- Return type:
int
- app.utilities.object_storage.generate_remote_file(bucket_name, folder_path, file_name, s3, content)[source]
Create a file directly in S3 with the specified content.
- Parameters:
bucket_name – The name of the S3 bucket.
folder_path – The folder path in the bucket.
file_name – The name of the file to create.
s3 – The boto3 S3 resource object.
content – The content to write to the file.
- Returns:
The S3 put_object response.
- Return type:
dict
- app.utilities.object_storage.timestamp()[source]
Generate a timestamp string for file naming.
- Returns:
Current timestamp in YYYYMMDDHHmmss format.
- Return type:
str
- app.utilities.object_storage.generate_upload_link_profile_picture(user, file_type)[source]
Generate a pre-signed upload URL for a user’s profile picture.
- Parameters:
user – The user object.
file_type – The MIME type of the image being uploaded.
- Returns:
JSON string containing upload data and preview URL.
- Return type:
str
- app.utilities.object_storage.generate_upload_link_validation_file(user, file_type, file)[source]
Generate a pre-signed upload URL for a batch validation file.
- Parameters:
user – The user object.
file_type – The MIME type of the file being uploaded.
file – The original filename.
- Returns:
JSON string containing upload data and preview URL.
- Return type:
str