0
votes

I'm writing a django rest project and recently deployed my project to google cloud. However, I'm having trouble uploading files.

Here is the response that I get:

400 POST https://storage.googleapis.com/upload/storage/v1/b/project-id/o?uploadType=multipart&predefinedAcl=publicRead: ('Request failed with status code', 400, 'Expected one of', <HTTPStatus.OK: 200>)

I have a pretty generic model and viewset as follows:

class EndUser(models.Model):
    id = models.AutoField(primary_key=True)
    image = models.ImageField(upload_to="users/", blank=True)
    name = models.CharField('Name', max_length=256)
    location = models.PointField()
    device = models.ForeignKey(FCMDevice, on_delete=models.CASCADE)
    current_task = models.ForeignKey('tasks.Task', on_delete=models.CASCADE, blank=True, null=True)

    def __str__(self):
        return self.name

class EndUserViewSet(viewsets.ModelViewSet):
    parser_classes = (MultiPartParser, FormParser)
    queryset = EndUser.objects.all()

    def get_serializer_class(self):
        if self.request.method == "GET":
            return EndUserSerializer
        return CreateUpdateEndUserSerializer

I believe my settings.py are prefect. But I'll add them here for reference:

MEDIA_ROOT = "https://storage.googleapis.com/my-project-id/media/"
STATIC_ROOT = "https://storage.googleapis.com/my-project-id/static/"
DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
STATICFILES_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
GS_BUCKET_NAME = "my-bucked-name"
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
    "credentials.json"
)

how do I solve this error?

1

1 Answers

1
votes

It was a very simple mistake...I had made access control uniform instead of fine grained and that caused all the mess. Even the documentation mentions. Reading the documentation will save you a lot of time :)

Ref: https://django-storages.readthedocs.io/en/latest/backends/gcloud.html