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_file(bucket_name, directory, file, s3[, s3path])

Upload a file to an S3 bucket.

download_file(bucket_name, directory, local_name, ...)

Download a file from an S3 bucket.

delete_file(bucket_name, keys, s3)

Delete one or more files from an S3 bucket.

list_objects(bucket, s3)

List all objects in an S3 bucket.

copy_file(source_bucket, destination_bucket, ...)

Copy a file between S3 buckets or within a bucket.

prevent_public_access(bucket, s3)

Configure a bucket to block all public access.

create_bucket(name, s3[, secure])

Create a new S3 bucket.

generate_download_link(bucket_name, key, s3[, ...])

Generate a pre-signed URL for downloading an S3 object.

generate_upload_link(bucket_name, file_name, file_type, s3)

Generate a pre-signed POST URL for uploading to S3.

folder_size(bucket_name, folder_path, s3)

Calculate the total size of all objects in a folder.

generate_remote_file(bucket_name, folder_path, ...)

Create a file directly in S3 with the specified content.

timestamp()

Generate a timestamp string for file naming.

generate_upload_link_profile_picture(user, file_type)

Generate a pre-signed upload URL for a user's profile picture.

generate_upload_link_validation_file(user, file_type, file)

Generate a pre-signed upload URL for a batch validation file.

generate_user_folder(user)

Create a user's personal folder in object storage.

user_folder_size(user)

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.

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

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

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

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

app.utilities.object_storage.generate_user_folder(user)[source]

Create a user’s personal folder in object storage.

Creates a placeholder file to establish the folder structure.

Parameters:

user – The user object.

app.utilities.object_storage.user_folder_size(user)[source]

Calculate the total size of a user’s folder.

Parameters:

user – The user object.

Returns:

Total size in bytes of the user’s folder.

Return type:

int