Azure / azure-storage-android

Microsoft Azure Storage Library for Android
Apache License 2.0
81 stars 47 forks source link

After uploading Image to blogstorage I am getting sas URL appended to image url #21

Closed ShreyashPromact closed 7 years ago

ShreyashPromact commented 8 years ago

Hello, I am using android azure storage to upload images. It was working fine with library version:

compile 'com.microsoft.azure.android:azure-storage-android:0.5.1@aar'

But, Now I have updated library to latest one that is as below:

compile 'com.microsoft.azure.android:azure-storage-android:0.7.0'

So, Now I got Image URL after uploading Image. That Image URL contain URL+SAS string with that. e.g: https://MYDOMAIN.blob.core.windows.net/images/5/cover/image_f279e54467224e1997788fda8e178c09.jpg?sv=2015-04-05&sr=c&sig=/gpTWMi9HZIP6xyw0no0d5sjAfo6Tx3rHRSOpjoB42w=&st=2015-12-21T07:37:41Z&se=2015-12-21T08:07:41Z&sp=w

So, I guess there is issue with the library that It gives SAS url along with Image URL.

Do I need to update the code which is working fine with the above library version or there is bug available in new storage library?

Please reply to me issue related to this. Thanks.

Sincerely, Shreyash

pemari-msft commented 8 years ago

Hi Shreyash,

A few questions:

Thanks! Peter

ShreyashPromact commented 8 years ago

Hello Peter.

Thanks for your reply. Please find below answers to your questions.

The Main thing is, I have just updated the library version from my project gradle, and all the image url after that having SAS in it, which was working fine before with old library.

Now, when is downgrade storage library to previous one, It seems like working fine. So I guess there is something changed in new updated library.

I want to be updated with the latest library but just because of this issue I am not. So, Please help me in this issue, of if there is something I need to update as per the new library, then please let me know.

Thanks. Sincerely, Shreyash

emgerner-msft commented 8 years ago

This isn't related to the sas token. This is a change in the library (hence the version change).

I'm guessing when you create the blob you're doing so from a container object using the getBlockBlobReference method and you pass a full URL as a string with the SAS token rather than just the blob name. We used to parse the URL for the blob name and sas token, but now we treat the URL as the blob name. This is because many users actually want the URL to be the blob name and there's no way to do that the way the library used to work.

The best way to do what you want now is to directly instantiate the CloudBlockBlob (or other blob type) with the constructor which takes a Java URL directly. This will parse the blob, container, and SAS from the URL (since it's a URL and not a string we know the intention) as was done before.

Since I can't see your code this is my guess. If it's not correct, please post the code snippet causing a problem.

Thanks! Emily

ShreyashPromact commented 8 years ago

@emgerner-msft

Thanks for reply. Please check below code snippet, I am using for uploading Image to my blob storage.

public static String uploadMedia(Context context, int SOME_ID, String SOME_OTHER_ID, String sas, String filePath){
        response = null;
        try {
            File source = new File(filePath); // File path
            String extantion = source.getAbsolutePath().substring(source.getAbsolutePath().lastIndexOf("."));
            String type = "image_";
            String uniqueID = type+ UUID.randomUUID().toString().replace("-", "")+extantion;
            String blobUri = context.getResources().getString(R.string.azure_upload_url) + SOME_ID+ "/story/" + SOME_OTHER_ID+ sas.replaceAll("\"","");
            StorageUri storage = new StorageUri(URI.create(blobUri));
            CloudBlobClient blobCLient = new CloudBlobClient(storage);
            CloudBlobContainer container = blobCLient.getContainerReference("");
            CloudBlockBlob blob = container.getBlockBlobReference(uniqueID);
            BlobOutputStream blobOutputStream = blob.openOutputStream();
            byte[] buffer = fileToByteConverter(source);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
            int next = inputStream.read();
            while (next != -1) {
                blobOutputStream.write(next);
                next = inputStream.read();
            }
            blobOutputStream.close();
            //======================================
            if(IS_DEBUG)
               Log.d(TAG, "Write operation succeeded for SAS " + sas + " URL: "+blob.getUri());
            response = "success:"+blob.getUri();
        } catch (StorageException e) {
            if(IS_DEBUG) {
               Log.d(TAG, "Additional error information: " + e.getMessage());
            }
            response = e.getMessage();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            response = e.getMessage();
        } catch (Exception e){
            e.printStackTrace();
            response = e.toString();
        }
        return response;
    }

This code works fine with azure blob storage library mention below: compile 'com.microsoft.azure.android:azure-storage-android:0.5.1@aar'

But Now when I just update library to latest version that is as below, it gives me wrong Image URL after uploading it to blob storage: compile 'com.microsoft.azure.android:azure-storage-android:0.7.0'

Please let me know whats the problem? Should I need to update code to work with latest library. If yes then why is it so?

Thanks. Sincerely, Shreyash

emgerner-msft commented 8 years ago

There's a couple aspects of this code that are a little odd. Lets clean those up and diagnose from that point:

  1. Rather than taking a file path, converting it to a file, converting the file to a buffer, putting the buffer in a stream, opening a blob output stream, and then looping, you could instead use uploadFromFile(final String path) which is a method on the blob. This will just take the file path and do a much more efficient job with much less code on your part.
  2. It doesn't look like you're using the client or the container object, and since the container string is on the original URI and you actually do getContainerReference with an empty string this is confusing and probably causing some bugs. Since you're not using the container or the client, just construct the full blob Uri you want to access and instantiate CloudBlockBlob directly as I mentioned in my original response.

Once you've made these changes lets take another look.

ShreyashPromact commented 8 years ago

OK. I will update that and will check it. I will also review that from you. As My app is live, I can not direct update it. but Will try it with some another way and will let you know for same.

Sincerely, Shreyash