Skip to content
Railway bucket does not allowing to put objects
coddermind
OP

10 months ago

follwing is the way im using to upload the image file to the railway bucket in django view:

from django.shortcuts import render
from .forms import ImageUploadForm
import boto3
from django.conf import settings
from django.contrib import messages

def home(request):
    image_url = None

    if request.method == "POST":
        form = ImageUploadForm(request.POST, request.FILES)
        if form.is_valid():
            img = request.FILES["image"]
            s3 = boto3.client(
                "s3",
                endpoint_url=settings.AWS_S3_ENDPOINT_URL,
                aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
                aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
                region_name="auto",
            )

            try:
                key = f"uploads/{img.name}"
                response = s3.put_object(
                    Bucket=settings.AWS_STORAGE_BUCKET_NAME,
                    Key=key,
                    Body=img.read(),
                    ContentType=img.content_type
                )
                print("UPLOAD RESPONSE:", response)
                # Generate signed URL for display
                image_url = s3.generate_presigned_url(
                    "get_object",
                    Params={"Bucket": settings.AWS_STORAGE_BUCKET_NAME, "Key": key},
                    ExpiresIn=3600
                )
                messages.success(request, f"Uploaded successfully: {key}")

            except Exception as e:
                messages.error(request, f"Upload failed: {str(e)}")
                print("UPLOAD ERROR:", e)

    else:
        form = ImageUploadForm()

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

and it says:

Upload failed: An error occurred (AccessDenied) when calling the PutObject operation: Access Denied.

0 Replies

Welcome!

Sign in to your Railway account to join the conversation.

Loading...