Azure / azure-storage-android

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

URL and sasQueryToken not generating #24

Closed shashi2459 closed 8 years ago

shashi2459 commented 8 years ago

I am following this doc https://azure.microsoft.com/en-in/documentation/articles/mobile-services-android-upload-data-blob-storage/ for uploading blob to azure cloud using Mobile Service. But url and sas token is not getting generated. any suggestions?

emgerner-msft commented 8 years ago

Please give the actual code you're using and what's going wrong. For example, is it throwing an error, or are you getting back a different token than you expect?

Basically, please answer: What did you do? What did you expect to happen? What actually happened?

shashi2459 commented 8 years ago

I am using the same upload() function from doc, except changed the class I am interested in. I want to upload the image in blob on azure cloud and I am using Azure Mobile Service for this. And as per the doc, I have configured my account on portal and I should get an object containing Generated URL and SASQueryString, but I am getting null values for URL and SAS string. Due to this, I am not able to proceed further and getting exception of Address should be absolute address since the SAS string is empty.

emgerner-msft commented 8 years ago

Please give the exact code you use. As you mentioned, you're not following it exactly -- you've changed the class and I'm not sure what that means. I need exactly the code you used that isn't working if I'm going to help debug since we know the sample works as is (we've run it).

shashi2459 commented 8 years ago

I mean I have changed the class name and added few fields and kept all the fields mentioned in the ToDoItem class. The insertion is working on EASY TABLE I have created on Azure cloud.

//My Blob class

public class Blob {

private String id;
private String productId;

@SerializedName("imageUri")
private String mImageUri;

@SerializedName("containerName")
private String mContainerName;

@SerializedName("resourceName")
private String mResourceName;

@SerializedName("sasQueryString")
private String mSasQueryString;

public Blob() {
    mContainerName = "";
    mResourceName = "";
    mImageUri = "";
    mSasQueryString = "";
}

public Blob(String id, String productId, String mImageUri, String mContainerName, String mResourceName, String mSasQueryString) {
    this.id = id;
    this.productId = productId;
    this.mImageUri = mImageUri;
    this.mContainerName = mContainerName;
    this.mResourceName = mResourceName;
    this.mSasQueryString = mSasQueryString;
}

public String getImageUri() {
    return mImageUri;
}

public final void setImageUri(String ImageUri) {
    mImageUri = ImageUri;
}

public String getContainerName() {
    return mContainerName;
}

public final void setContainerName(String ContainerName) {
    mContainerName = ContainerName;
}

public String getResourceName() {
    return mResourceName;
}

public final void setResourceName(String ResourceName) {
    mResourceName = ResourceName;
}

public String getSasQueryString() {
    return mSasQueryString;
}

public final void setSasQueryString(String SasQueryString) {
    mSasQueryString = SasQueryString;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getProductId() {
    return productId;
}

public void setProductId(String productId) {
    this.productId = productId;
}

@Override
public String toString() {
    return "Blob{" +
            "id='" + id + '\'' +
            ", productId='" + productId + '\'' +
            ", mImageUri='" + mImageUri + '\'' +
            ", mContainerName='" + mContainerName + '\'' +
            ", mResourceName='" + mResourceName + '\'' +
            ", mSasQueryString='" + mSasQueryString + '\'' +
            '}';
}
}`

//my upload blob function public void uploadPhoto(final String mPhotoFileUri) { if (mClient == null) { return; } Log.e("uploadPhoto", "Uploading");

    // Create a new item
    final Blob item = new Blob();

    item.setProductId(product.getServerId());
    item.setContainerName("productimages");

    // Use a unigue GUID to avoid collisions.
    UUID uuid = UUID.randomUUID();
    String uuidInString = uuid.toString();
    item.setResourceName(uuidInString);

    // Send the item to be inserted. When blob properties are set this
    // generates an SAS in the response.
    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                final Blob entity = addItemInTable(item);
                Log.e("ToDoItem", entity.toString());
                // If we have a returned SAS, then upload the blob.
                String queryString = entity.getSasQueryString();
                if (!queryString.equals("")) {
                    Log.e("QueryString", queryString+" not null");
                    StorageCredentials cred = new StorageCredentialsSharedAccessSignature(queryString);
                    URI imageUri = new URI(entity.getImageUri());

                    // Upload the new image as a BLOB from a file.
                    CloudBlockBlob blobFromSASCredential = new CloudBlockBlob(imageUri, cred);

                    blobFromSASCredential.uploadFromFile(mPhotoFileUri);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(DetailsActivity.this, "Blob uploaded.", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            } catch (final Exception e) {
                Log.e("Exception", e.getMessage());
                Throwable ex;
                if (e.getCause() != null) {
                    ex = e.getCause();
                    Log.e("Exception", ex.getCause() + "");
                }
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(DetailsActivity.this, "Blob not uploaded.", Toast.LENGTH_SHORT).show();
                    }
                });
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            progressBar.setVisibility(View.GONE);
        }
    };

    runAsyncTask(task);
}`
emgerner-msft commented 8 years ago

Thanks! How are you actually populating the class? Calling getImageUri and getSasQueryString just return whatever is set on the item. The code which actually generates shared access signatures is much earlier in the sample.

shashi2459 commented 8 years ago

Class population is in uploadPhoto(). So empty values are assigned to uri and SAS string. Same is done in the sample.

emgerner-msft commented 8 years ago

I think there's a bit of a misunderstanding on how the sample works, not really the library. Can you please post something like that question on the sample itself? The author will better be able to help you.

shashi2459 commented 8 years ago

ok...Thanks by the way.

On Tue, May 3, 2016 at 12:38 AM, Emily Gerner notifications@github.com wrote:

I think there's a bit of a misunderstanding on how the sample works, not really the library. Can you please post something like that question on the sample itself? The author will better be able to help you.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/Azure/azure-storage-android/issues/24#issuecomment-216331401

Regards,

[image: --]

Shashikant Sule [image: https://]about.me/shashi2459 8793356590 https://about.me/shashi2459?promo=email_sig

emgerner-msft commented 8 years ago

Yup! I'll close this issue and email the author to make sure he sees your message.