3

I'm trying to save an image to our Azure blog storage. Thanks to the helpful response provided via the link below the code snippet, I was able to successfully save the file to our top-level container. Unfortunately, I would like the file to save to a subdirectory in that container. And, for the life of me, I can not get it to work.

Currently, the image is saving to our "images" container. Within that container is a folder, "members". I would like the files to save to that subdirectory. So "images/members". I tried to pass "images/members" to GetBlockBlobReference but then the file just didn't save at all (or, at least I can't find it).

This seems like it should be pretty simple. Thanks in advance.

  CloudBlobClient blobClient = account.CreateCloudBlobClient();
  CloudBlobContainer container = blobClient.GetContainerReference("images");
  CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
  blockBlob.UploadFromStream(stream);

Top-level container. The image with the Guid is one that I uploaded

The "members" directory. Sorted by most recent; nothing recent appearing

Helpful solution that got me to saving successfully to the top-level container

BoGoodSki
  • 123
  • 1
  • 4
  • 12
  • when you tried "images/members" what happened? what do you use to view your containers and blobs. this one: https://azure.microsoft.com/en-us/features/storage-explorer/? – Saher Ahwal Apr 09 '18 at 21:57
  • don't change container reference, keep that to images. You need to change block blob reference to "members/filename" – Saher Ahwal Apr 09 '18 at 21:58
  • Using Azure Storage Explorer to view the container. Members is an existing "directory" with many images already saved to it. Before changing the code as suggested, I would see my uploaded file saved to the images container (in Storage Explorer). When I made the change to code suggested and then navigate to the members "directory", I see all of the old images. I resort by "Last Modified" by nothing recent appears at the top of the list of files. Thanks again! – BoGoodSki Apr 09 '18 at 22:21
  • Use listBlobs call with prefix "members/" under container "images" and you should be able to get all blobs created under members folder. That's what storage explorer does when it shows you the files. – Saher Ahwal Apr 09 '18 at 22:30

3 Answers3

6

"images" is the name of your container.

what you need to do is change this line from

CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);

to

CloudBlockBlob blockBlob = container.GetBlockBlobReference("members/" + filename);

Then you can use the Azure Storage Explorer to view your files and folders: https://azure.microsoft.com/en-us/features/storage-explorer/

Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152
  • I feel like this is the right answer. But, I made the suggested change and I'm still not seeing the image in the members "folder". Like, I feel like we're just right on the edge of making this work, but not all the way there. This solution makes sense, though! Thanks! – BoGoodSki Apr 09 '18 at 22:13
  • If this solved your problem, then please @BoGoodSki either upvote / accept answer or both. Thank you! – Saher Ahwal Apr 09 '18 at 22:56
  • I've upvoted (but don't have enough rep for it be recorded). I didn't accept it as the solution because the files still aren't making it to "members". Not quite sure what's going on. The solution makes sense, and seems to me like it should work, but just isn't. Once I get it functioning, I'll accept the answer. – BoGoodSki Apr 09 '18 at 23:00
  • It's working now. I accepted this solution. Apparently, I just had to click on "Load More" in Storage Explorer about 1000 times to get the recently added images to bubble to the top. – BoGoodSki Apr 09 '18 at 23:06
  • @BoGoodSki I'm glad it's working. Yes the Storage explorer calls ListBlob with some max value and you will need to load more as appropriate. Thank you! – Saher Ahwal Apr 10 '18 at 00:23
3

Using a slash (/) in the filename will create a 'folder'. I used 'folder' because it's a virtual folder, a trick used to give us humans the idea of folders. There's actually only one level of grouping which is the Container.

Each slash (/) in a filename stands for one 'folder', so creating a blob with filename firstFolder/secondFolder/filename.txt will create a file with that exact name. Which looks like a file with the path firstFolder -> secondFolder. You can ask a container to ListBlobs with useFlatBlob set to true, returning you all blobs in the specific container. So all Blobs in all folders.

You can also ask for all blobs in a virtual folder by getting a DirectoryReference using CloudBlobContainer.GetDirectoryReference and listing the blobs under there.

More info here: Work with Blob resources

rickvdbosch
  • 14,105
  • 2
  • 40
  • 53
0

You can follow this snip of code to do the same.

output = detected_anomaly.to_csv (encoding = "utf-8", index=False)
blob_path = 'blobfolder1/blobfolder2/' 
blob_path_anomalies = blob_path + '<created_blob_folder_name>/demo.csv'
blob_service.create_blob_from_text(container_name, blob_path_anomalies, output)
Palash Mondal
  • 468
  • 4
  • 10