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

Android Azure Storage Blob upload with SAS; com.microsoft.azure.storage.StorageException: The specified resource does not exist

我正在尝试使用 Microsoft Azure Storage SDK for Android and Azure Mobile service.

从 android 将文件作为 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.doInBackground(ToDoActivity.java:253)
08-18 19:05:04.297  11595-11711/com.example.zumoimageupload W/System.err﹕ at com.example.mcettodolist.ToDoActivity.doInBackground(ToDoActivity.java:226)
08-18 19:05:04.297  11595-11711/com.example.zumoimageupload W/System.err﹕ at android.os.AsyncTask.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)

StorageCredentials.tryParseCredentials() 采用连接字符串,因此我认为这不是正确的方法。尝试使用采用 SAS 令牌的 StorageCredentialsSharedAccessSignature 构造函数直接创建您想要的凭据对象。

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