Skip to main content

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

  1. Install the required dependencies by running the following command:
   pip install MT-PY-File-upload 
  1. 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_PY_FileUpload.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

  1. Install the required dependencies by running the following command:
   pip install MT-PY-File-upload 
  1. 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_PY_FileUpload.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_PY_FileUpload 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_PY_FileUpload.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_PY_FileUpload package in mysqlite"})    
    else:
        form =Doc_fields()
        return render(request,"local_upload/index.html",{"form":form,
        "header":"Saving file,image using MT_PY_FileUpload 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 Distribution

MT_PY_File_upload-0.2.4.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

MT_PY_File_upload-0.2.4-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file MT_PY_File_upload-0.2.4.tar.gz.

File metadata

  • Download URL: MT_PY_File_upload-0.2.4.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for MT_PY_File_upload-0.2.4.tar.gz
Algorithm Hash digest
SHA256 449ad558d02bf85f8ff1cc4850ac0586a91ddb37a174a4961d1900768f1362c7
MD5 8d4af2c59bfa0360eedff0657428c4f0
BLAKE2b-256 a03c7fac59bc76cd5a797f06a2f3082e96e299982d01e7c16154cbf30e5d486f

See more details on using hashes here.

File details

Details for the file MT_PY_File_upload-0.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for MT_PY_File_upload-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 97d65fbf08cdb79a1886eaf9469d4eeb3905fa047d70848640f32005dc8d173d
MD5 a24072e24d3e927774c5d0229eac56ab
BLAKE2b-256 a2f5183a2511d1bf6ef5d87c9accf261f6bffc39594e50ffb3cef23a7965c6d6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page