2👍
The Blobs in Azure Blob Storage have their own unique urls for accessing. The URL is in the format: http://<your_storage_name>.blob.core.windows.net/<container_name>/<blob_name>
, you can directly access it in browser, if you set the access permission of blob to public.
To your issue, if it is not sensitive to your images in Blob Storage, you can simply set access permission to public blob
to allow public read access to the blobs in the container, but not the container properties and metadata.
Login in Azure mange portal, click storage tab in left nav, click your storage name in the list to step in your storage manage page, click CONTAINERS tab, select the specific container name, click the EDIT button in bottom, change the access permission and click OK button to save the configuration:
Click the container name we can step in the list of blobs in this container. We can copy the URL of an item, visit in browser to have a check.
And per my understanding, if you want to show the images after uploading to Azure storage, we just need a few modification on the original code.
In the custom storage class, assume the function url()
should return the correct the URL. In my test, I directly return the URL string for a quick test:
def geturl(self,name):
return '%s/%s/%s' % ('http://garyteststorage.blob.core.windows.net','mycontainer', name)
And we can modify the return of function _save()
to the URL
of the image class instead of name
:
url = self.geturl(name)
return url
#return name
In models.py:
def upload_path(instance, filename):
return 'uploads-from-custom-storage-{}'.format(filename)
class Photo(models.Model):
#description = models.TextField(validators=[MaxLengthValidator(500)])
#submitted_on = models.DateTimeField(auto_now_add=True)
image_file = models.ImageField(upload_to=upload_path, storage=OverwriteStorage(), null=True, blank=True )
As before, it will save the image name in database, and after modification, it will save the full url of blob in database.
code snippet In view.py:
if form.is_valid():
newImage = Photo(image_file = request.FILES['image_file'])
newImage.save()
imageurl = newImage.image_file
html = "<img src=%s></img><br>" %imageurl
# Redirect to the document list after POST
return HttpResponse(html)