A package to implementing file upload in AWS s3 bucket and store mysqlite,msql,postgress,Azure Blob
Project description
MT-PY-File-upload
Upload File to AWS S3
This script demonstrates how to upload a file to AWS S3 using the
boto3
library in Python.
Prerequisites
- Python 3.x
- AWS account with S3 access
- AWS credentials configured (via environment variables or AWS CLI)
Setup
- Install the required dependencies by running the following command:
pip install MT-PY-File-upload
- Some support-package to work with file upload package.
pip instal boto3 python-dotenv
- Create a .env file in the same directory as the script and provide the necessary AWS credentials and region:
AWS_ACCESS_KEY_ID=your-access-key-id
AWS_SECRET_ACCESS_KEY=your-secret-access-key
AWS_S3_REGION_NAME=your-s3-region
AWS_STORAGE_BUCKET_NAME=bucket-name
Usage
Import the necessary libraries:
import boto3
import os
from mt_django_file_upload.upload_file_to_s3 import Upload_file_to_s3
from dotenv import load_dotenv
load_dotenv()
- Define the function Upload_file_to_s3 to upload a file to S3:
Upload_file_to_s3(file_path, bucket_name, object_name,aws_access_key,
aws_secret_key,region_name )
- Call the function with the required parameters:
Upload_file_to_s3("path/to/file", "your-bucket-name", "object-key-name",
"aws_access_key","aws_secret_key","region_name")
References
boto3 documentation python-dotenv documentation
Azure Blob Storage Upload Package
This package provides functions to upload files to Azure Blob Storage.
Installation
You can install this package using pip:
Prerequisites
- Python 3.x
- Azure account
- storage accounts
- create container with blog storage facility.
Setup
- Install the required dependencies by running the following command:
pip install MT-PY-File-upload
- Some support-package to work with file upload package.
pip instal azure-core azure-storage-blob python-dotenv
- Create a .env file in the same directory as the script and provide the necessary AWS credentials and region:
AZURE_STORAGE_ACCOUNT_KEY='azure-account-key'
AZURE_STORAGE_ACCOUNT_NAME="storage-name"
AZURE_CONTAINER_NAME="contaier-name"
AZURE_CONNECTION_STRING="Connection string"
Usage
Import the necessary libraries:
import os
from mt_django_file_upload.azureBlobStorage import uploadToBlobStorage
from dotenv import load_dotenv
load_dotenv()
-
Define the function uploadToBlobStorage to upload a file to S3:
-
Call the function with the required parameters:
uploadToBlobStorage("path/to/file", "your-bucket-name", "object-key-name",
"aws_access_key","aws_secret_key","region_name")
Parameters:
- file_path (str): Path to the file you want to upload.
- file_name (str): Name to give to the file in the storage.
- storage_account_key (str): Your Azure Storage Account key.
- storage_account_name (str): Your Azure Storage Account name.
- container_name (str): Name of the container in Azure Blob Storage.
- connection_string (str): Azure Storage Account connection string.
Example
from mt_django_file_upload import azureBlobStorage
file_path = "path/to/your/file.txt"
file_name = "example_file.txt"
storage_account_key = "YOUR_STORAGE_ACCOUNT_KEY"
storage_account_name = "YOUR_STORAGE_ACCOUNT_NAME"
container_name = "YOUR_CONTAINER_NAME"
connection_string = "YOUR_CONNECTION_STRING"
uploadToBlobStorage(file_path, file_name, storage_account_key,
storage_account_name, container_name, connection_string)
References
azure core documentation python-dotenv documentation
Custom FileField with Extension Validation
This code provides custom file field classes for Django models with extension validation. It extends Django's built-in FileField and adds additional validation for allowed file extensions.
Prerequisites
- Django framework
Usage
Mt_FileField
A custom FileField that validates the file extension based on a list of allowed extensions.
Example usage:
from django.db.models import FileField
from django.core.validators import FileExtensionValidator
class Mt_FileField(FileField):
def __init__(self, *args, allowed_extensions=None, **kwargs):
self.allowed_extensions = allowed_extensions or []
super().__init__(*args, **kwargs)
def validate(self, value, model_instance):
super().validate(value, model_instance)
# Use Django's FileExtensionValidator to validate the file extension
validator = FileExtensionValidator(allowed_extensions=self.allowed_extensions)
# Validate the file
try:
validator(value)
except ValidationError as e:
# If the file is not valid, raise a ValidationError with a custom error message
raise ValidationError('Invalid file format. Only {} files are allowed.'.format(
', '.join(self.allowed_extensions))
) from e
Custom FileField with Extension Validation
This code provides custom file field classes for Django models with extension validation. It extends Django's built-in FileField and adds additional validation for allowed file extensions.
Usage
Mt_FileField
A custom FileField that validates the file extension based on a list of allowed extensions.
implement in models.py
Example:
from mt_django_file_upload.file_upload import Mt_fileUploadField,Mt_FileField
class SaveFile(models.Model):
file1=Mt_fileUploadField(upload_to="newDoc",null=True,blank=True)
# Yo can limit your file fields
file2=Mt_FileField(upload_to="doc",
null=True,blank=True,allowed_extensions=['txt','doc','docx'])
implement in views.py
Example:
def home(request,*args,**kwargs):
if(request.method=="POST"):
filled_form=Doc_fields(request.POST,request.FILES)
note=''
header = "Upload folder form"
if(filled_form.is_valid()):
filled_form.save()
note=f"{filled_form.cleaned_data['text']} item was saved successfully"
else:
note="Somthing went wrong"
return render(request,'local_upload/index.html',
{"note":note,"form":
filled_form,"header":"Save file,image using mt_django_file_upload package in mysqlite"})
else:
form =Doc_fields()
return render(request,"local_upload/index.html",{"form":form,
"header":"Saving file,image using mt_django_file_upload package in mysqlite"})
def example(request):
context={
"hello":_("Hello")
}
return render(request,'local_upload/example.html',context)
implement in settings.py
Example:
MEDIA_ROOT=BASE_DIR/'media'
MEDIA_URL='/media/'
- You can change database by changing database configuration in settings.py
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
from django.db.models import FileField
from django.core.validators import FileExtensionValidator
class Mt_FileField(FileField):
def __init__(self, *args, allowed_extensions=None, **kwargs):
self.allowed_extensions = allowed_extensions or []
super().__init__(*args, **kwargs)
def validate(self, value, model_instance):
super().validate(value, model_instance)
# Use Django's FileExtensionValidator to validate the file extension
validator = FileExtensionValidator(allowed_extensions=self.allowed_extensions)
# Validate the file
try:
validator(value)
except ValidationError as e:
# If the file is not valid, raise a ValidationError with a custom error message
raise ValidationError('Invalid file format. Only {} files are allowed.'.format(
', '.join(self.allowed_extensions))
) from e
Mt_form_fileUploadField
A custom form file field that extends Django's built-in FileField.
Example usage:
from django.forms import FileField as f_FileField
class Mt_form_fileUploadField(f_FileField):
pass
Mt_fileUploadField
A custom file field that extends Django's built-in FileField.
Example usage:
from django.db.models import FileField
class Mt_fileUploadField(FileField):
pass
References
Django FileField documentation Django FileExtensionValidator documentation
This documentation provides an overview of the custom field classes and how to use them in
your Django models or forms. It also includes references to the relevant Django documentation
for further details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file MT_PY_File_upload-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: MT_PY_File_upload-0.0.4-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5535b205551ab7373c9c9f328e5d91c5c62980986fafe1b686a5cb12cd8a5ae5 |
|
MD5 | 2a1222c7e54fd48371276eadca88fcb8 |
|
BLAKE2b-256 | 212727207530a1945b9ae04f8cc6d81fbc75e2d98cd7d9b8cb27b461a709cb6f |