使用SAS上传Android Azure Storage Blob; com.microsoft.azure.storage.StorageException:指定的资源不存在

时间:2015-08-18 13:58:10

标签: android azure-storage azure-mobile-services azure-storage-blobs azure-android-sdk

我正在尝试使用Microsoft Azure Storage SDK for AndroidAzure Mobile service将文件作为blob上传到Azure存储。

实际上我无法使用android中的共享访问签名(SAS)找到任何示例代码。上面提到的移动服务教程适用于.NET应用程序。 所以按照相同的教程,我试图在android中创建一个示例应用程序用于图像上传

这是我的android代码:

 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>(){
        @Override
        protected Void doInBackground(Void... params) {
            try {
                final ImageItem entity = addItemInTable(item);
                Log.d("InsertedItem:", entity.toString());

                StorageCredentials cred = StorageCredentials.tryParseCredentials(entity.getSasQueryString());

                URI imageURI = new URI(entity.getImageUri());

                URI containerUri = new URI("https://" + imageURI.getHost() + "/" + entity.getContainerName());
                Log.d("ContainerURI:", containerUri.toString());

                CloudBlobContainer container = new CloudBlobContainer(containerUri, cred);

                CloudBlockBlob blobFromSASCredential = container.getBlockBlobReference(entity.getResourceName());
                AssetManager assManager = getApplicationContext().getAssets();
                InputStream is = null;
                try {
                    is = assManager.open("image.jpg");

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                blobFromSASCredential.upload(is, -1);
              //blobFromSASCredential.uploadText("Sample text");


                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!entity.isComplete()) {
                            mAdapter.add(entity);
                        }
                    }
                });
            } catch (final Exception e) {
                e.printStackTrace();
                createAndShowDialogFromTask(e, "Error");
            }
            return null;
        }
    };

public ImageItem addItemInTable(ImageItem item) throws ExecutionException, InterruptedException {
    item.setContainerName("todoitemimages");
    item.setResourceName(UUID.randomUUID().toString());

    ImageItem entity = mToDoTable.insert(item).get();
    return entity;
}

这是我的GSON对象:

public class ImageItem {
/**
 * imageUri : sample string 6
 * containerName : sample string 3
 * sasQueryString : sample string 5
 * resourceName : sample string 4
 * text : sample string 1
 * id : sample string 7
 * complete : true
 */
@SerializedName("imageUri")
private String imageUri;
@SerializedName("containerName")
private String containerName;
@SerializedName("sasQueryString")
private String sasQueryString;
@SerializedName("resourceName")
private String resourceName;
@SerializedName("text")
private String text;
@SerializedName("id")
private String id;
@SerializedName("complete")
private boolean complete;
}

运行代码时出现此错误:

08-18 19:05:02.650  11595-11711/com.example.zumoimageupload D/InsertedItem:﹕ ID:58b3722d9d164ff6aae3cbf35b0ccfa7 Text:demo text Completefalse ContainerName:todoitemimages ResourceName:c8041429-3269-4531-87f3-4e24652a3005 ImageUri:https://storageserverdemo.blob.core.windows.net/todoitemimages/c8041429-3269-4531-87f3-4e24652a3005 SasQueryString:?sv=2015-02-21&sr=c&sig=iPRd%2BVROWcOtSvSaTNyLjAKwDmyCG%2B1QEr%2BAPEMLKJ8%3D&st=2015-08-18T13%3A35%3A02Z&se=2015-08-18T13%3A40%3A02Z&sp=w
08-18 19:05:04.253  11595-11711/com.example.zumoimageupload W/System.err﹕ com.microsoft.azure.storage.StorageException: The specified resource does not exist.
08-18 19:05:04.287  11595-11711/com.example.zumoimageupload W/System.err﹕ at com.microsoft.azure.storage.core.StorageRequest.materializeException(StorageRequest.java:305)
08-18 19:05:04.289  11595-11711/com.example.zumoimageupload W/System.err﹕ at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:175)
08-18 19:05:04.289  11595-11711/com.example.zumoimageupload W/System.err﹕ at com.microsoft.azure.storage.blob.CloudBlockBlob.uploadFullBlob(CloudBlockBlob.java:826)
08-18 19:05:04.289  11595-11711/com.example.zumoimageupload W/System.err﹕ at com.microsoft.azure.storage.blob.CloudBlockBlob.upload(CloudBlockBlob.java:779)
08-18 19:05:04.290  11595-11711/com.example.zumoimageupload W/System.err﹕ at com.microsoft.azure.storage.blob.CloudBlob.uploadFromByteArray(CloudBlob.java:1921)
08-18 19:05:04.290  11595-11711/com.example.zumoimageupload W/System.err﹕ at com.microsoft.azure.storage.blob.CloudBlockBlob.uploadText(CloudBlockBlob.java:1112)
08-18 19:05:04.291  11595-11711/com.example.zumoimageupload W/System.err﹕ at com.microsoft.azure.storage.blob.CloudBlockBlob.uploadText(CloudBlockBlob.java:1082)
08-18 19:05:04.296  11595-11711/com.example.zumoimageupload W/System.err﹕ at com.example.mcettodolist.ToDoActivity$2.doInBackground(ToDoActivity.java:253)
08-18 19:05:04.297  11595-11711/com.example.zumoimageupload W/System.err﹕ at com.example.mcettodolist.ToDoActivity$2.doInBackground(ToDoActivity.java:226)
08-18 19:05:04.297  11595-11711/com.example.zumoimageupload W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:292)
08-18 19:05:04.297  11595-11711/com.example.zumoimageupload W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-18 19:05:04.297  11595-11711/com.example.zumoimageupload W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-18 19:05:04.297  11595-11711/com.example.zumoimageupload W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-18 19:05:04.297  11595-11711/com.example.zumoimageupload W/System.err﹕ at java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:2)

StorageCredentials.tryParseCredentials()接受一个连接字符串,所以这不是我想的正确方法。尝试使用带有SAS令牌的StorageCredentialsSharedAccessSignature构造函数直接创建所需的凭证对象。

此外,为了上传blob,您需要确保将要上传的容器存在。如果你确定它确实如此,那么这段代码就是好的。否则,容器对象上的create()或createIfNotExists()方法就是你需要的。