Google Cloud Storage (GCS) + Django - File Upload, Download, List, and Delete Example

To integrate Google Cloud Storage (GCS) with a Django application for file upload, download, list, and delete operations, follow these steps:


1. Set Up Google Cloud Storage

  1. Create a Google Cloud Project:

  2. Enable the Cloud Storage API:

    • Navigate to APIs & Services > Library, search for "Cloud Storage," and enable it.
  3. Create a GCS Bucket:

    • Go to Storage > Browser in the console.
    • Click Create Bucket, choose a globally unique name, configure settings, and create it.
  4. Set Up a Service Account:

    • Go to IAM & Admin > Service Accounts, create a service account, and assign the Storage Admin role.
    • Generate a key (JSON) for the service account and download it to your local machine.

2. Configure Django for GCS

  1. Install Required Libraries: Install the Google Cloud Storage and Django libraries:

    pip install google-cloud-storage django-storages
  2. Set Up the Django Storage Backend: Add the storages app to your Django project:

    INSTALLED_APPS = [
        ...
        "storages",
    ]
  3. Configure Django Settings: Add the following configuration in your settings.py:

    import os
    from google.oauth2 import service_account
    
    # GCS Bucket Name
    GS_BUCKET_NAME = "your-bucket-name"
    
    # Path to Service Account Key JSON
    GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
        os.path.join(BASE_DIR, "path/to/your-service-account-key.json")
    )
    
    # GCS Static/Media File Storage
    DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
    STATICFILES_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
    
    # GCS Custom Domain (optional)
    GS_CUSTOM_DOMAIN = f"{GS_BUCKET_NAME}.storage.googleapis.com"
    
    # Media Settings
    MEDIA_URL = f"https://{GS_CUSTOM_DOMAIN}/media/"
    MEDIA_ROOT = "media/"
  4. Prepare Your Bucket:

    • Make your bucket publicly accessible for media files (optional) or set up IAM permissions to allow authenticated access.

3. Django Views for File Operations

Here’s an example of how to implement file upload, list, download, and delete operations:

Upload File

Create a form and view for uploading files:

# forms.py
from django import forms

class FileUploadForm(forms.Form):
    file = forms.FileField()

# views.py
from django.shortcuts import render
from google.cloud import storage
from django.conf import settings
from .forms import FileUploadForm

def upload_file(request):
    if request.method == "POST":
        form = FileUploadForm(request.POST, request.FILES)
        if form.is_valid():
            # Get the uploaded file
            file = form.cleaned_data["file"]

            # Initialize GCS client
            client = storage.Client(credentials=settings.GS_CREDENTIALS)
            bucket = client.bucket(settings.GS_BUCKET_NAME)

            # Upload the file
            blob = bucket.blob(f"media/{file.name}")
            blob.upload_from_file(file)

            return render(request, "upload_success.html", {"filename": file.name})

    else:
        form = FileUploadForm()

    return render(request, "upload.html", {"form": form})

List Files

# views.py
def list_files(request):
    client = storage.Client(credentials=settings.GS_CREDENTIALS)
    bucket = client.bucket(settings.GS_BUCKET_NAME)

    # List all files in the "media/" folder
    blobs = bucket.list_blobs(prefix="media/")
    files = [blob.name for blob in blobs]

    return render(request, "list_files.html", {"files": files})

Download File

# views.py
from django.http import HttpResponse

def download_file(request, file_name):
    client = storage.Client(credentials=settings.GS_CREDENTIALS)
    bucket = client.bucket(settings.GS_BUCKET_NAME)

    # Get the file blob
    blob = bucket.blob(f"media/{file_name}")

    # Download the file to memory
    file_content = blob.download_as_bytes()

    # Create a response
    response = HttpResponse(file_content, content_type="application/octet-stream")
    response["Content-Disposition"] = f'attachment; filename="{file_name}"'

    return response

Delete File

# views.py
from django.shortcuts import redirect

def delete_file(request, file_name):
    client = storage.Client(credentials=settings.GS_CREDENTIALS)
    bucket = client.bucket(settings.GS_BUCKET_NAME)

    # Get the file blob and delete it
    blob = bucket.blob(f"media/{file_name}")
    blob.delete()

    return redirect("list_files")

4. Django Templates

Upload File Form

upload.html:

<form method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Upload</button>
</form>

List Files

list_files.html:

<h1>Uploaded Files</h1>
<ul>
    {% for file in files %}
        <li>
            {{ file }}
            <a href="{% url 'download_file' file_name=file %}">Download</a>
            <a href="{% url 'delete_file' file_name=file %}">Delete</a>
        </li>
    {% endfor %}
</ul>

5. URLs

Configure the URLs for the views:

# urls.py
from django.urls import path
from . import views

urlpatterns = [
    path("upload/", views.upload_file, name="upload_file"),
    path("files/", views.list_files, name="list_files"),
    path("download/<str:file_name>/", views.download_file, name="download_file"),
    path("delete/<str:file_name>/", views.delete_file, name="delete_file"),
]

6. Run and Test

  1. Start your Django development server:
    python manage.py runserver
  2. Navigate to /upload/ to upload files.
  3. Navigate to /files/ to list, download, or delete files.

7. Key Points

  • Replace your-bucket-name and path/to/your-service-account-key.json with the correct values.
  • Ensure proper IAM permissions for file operations (e.g., storage.objectAdmin for the service account).
  • For production, consider using a private bucket and serve files via signed URLs or a CDN.

This setup integrates Google Cloud Storage with Django, providing a seamless way to manage files in your application.

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete