d1spstack
d1spstack

Reputation: 1380

google cloud storage images differ for authenticated url and public url

I cant seem to understand how is it possible that for GCS the authenticated URL shows a different image then the public URL ?

Im uploading the images via a python django script

def upload_to_cloud(blob_name, file_obj):
    file_type = imghdr.what(file_obj)
    blob_name = str(blob_name) + '.' + file_type  # concatenate string to create 'file_name.format'
    stats = storage.Blob(bucket=bucket, name=blob_name).exists(client)  # check if logo with the same reg.nr exists
    if stats is True:  # if exists then delete before uploading new logo
        storage.Blob(bucket=bucket, name=blob_name).delete()
    blob = bucket.blob(blob_name)
    blob.upload_from_file(file_obj=file_obj, content_type=f'image/{file_type}')
    path = blob.public_url
    return path

class CompanyProfile(SuccessMessageMixin, UpdateView):  # TODO why company logo differs from the one in ads_list?
    model = Company
    form_class = CompanyProfileCreationForm

    def form_valid(self, form):
        """
        Check if user uploaded a new logo. If yes
        then upload the new logo to google cloud
        """
        if 'logo' in self.request.FILES:
            blob_name = self.request.user.company.reg_nr  # get company registration number
            file_obj = self.request.FILES['logo']  # store uploaded file in variable
            form.instance.logo_url = upload_to_cloud(blob_name, file_obj)  # update company.logo_url with path to uploaded file
            company = Company.objects.get(pk=self.request.user.company.pk)
            company.save()
            return super().form_valid(form)
        else:
            return super().form_valid(form)

Any ideas on what Im doing wrong and how its even possible? The file that I actually uploaded is the one under authenticated url. The file thats under public url is a file that I uploaded for a different blob

EDIT

Im adding screenshot of the different images because after some time the images appear to be the same as they should be. Some people are confused by this and comment that the images are the same after all

Public URL public url

Authenticated URL authenticated url

Note that caching issue is ruled out since I sent the public URL to my friend and he also saw that the image is the HTML text although the image in the authenticated URL (the correct image) was a light bulb. He also noted that the URL preview in fb messenger showed the light bulb image but when he actually opened the URL the HTML text image appeared

This problem persists in case a file is uploaded with the same blob name. This happens regardless if its overwritten by gcs or if I previously execute blob delete function and then create a new file with the same name as the deleted blob.

Upvotes: 1

Views: 2119

Answers (2)

Hassaan Shariq Mirza
Hassaan Shariq Mirza

Reputation: 31

The Google is caching, try appending ?v=1.0 at the end of file name and it will not pick caching file.

Upvotes: 3

David
David

Reputation: 9721

In general the same object will be served by storage.googleapis.com and storage.cloud.google.com.

The only exception is if there is some caching (either in your browser, in a proxy, with Cloud CDN or in GCS). If you read the object via storage.cloud.google.com before uploading a new version, then reading after by storage.cloud.google.com may serve the old version while storage.googleapis.com returns the new one. Caching can also be location dependent.

If you can't allow an hour of caching, set Cache control to no-cache.

Upvotes: 1

Related Questions