【发布时间】:2015-10-29 02:10:28
【问题描述】:
我知道有可用于使用 Azure 存储上传文件的库。我已经参考了this。
但是,他们没有提供有关如何使用 SAS 的信息。我有帐户名和用于访问和上传文件的 sas url。但我不知道如何使用它来上传文件。
如果我使用上面提到的库,它会显示无效的存储连接字符串,因为我没有在其中传递密钥(sas 不需要)。所以我很困惑如何上传文件。
我也参考了this 文档以使用 sas 上传文件。但没有得到适当的步骤来做到这一点。他们已经为他们的 windows 应用程序制作了演示。我想在 android 中使用 sas。
更新
我参考console app made by Azure 尝试使用以下代码来检查和访问 SAS。
try {
//Try performing container operations with the SAS provided.
//Return a reference to the container using the SAS URI.
//CloudBlockBlob blob = new CloudBlockBlob(new StorageUri(new URI(sas)));
String[] str = userId.split(":");
String blobUri = "https://myStorageAccountName.blob.core.windows.net/image/" + str[1] + "/story/" + storyId + "/image1.jpg" + sas.toString().replaceAll("\"","");
Log.d(TAG,"Result:: blobUrl 1 : "+blobUri);
CloudBlobContainer container = new CloudBlobContainer(new URI(blobUri));
Log.d(TAG,"Result:: blobUrl 2 : "+blobUri);
CloudBlockBlob blob = container.getBlockBlobReference("image1.jpg");
String filePath = postData.get(0).getUrl().toString();
/*File source = new File(getRealPathFromURI(getApplicationContext(),Uri.parse(filePath))); // File path
blob.upload(new FileInputStream(source), source.length());*/
Log.d(TAG,"Result:: blobUrl 3 : "+blobUri);
//blob.upload(new FileInputStream(source), source.length());
//blob.uploadText("Hello this is testing..."); // Upload text file
Log.d(TAG, "Result:: blobUrl 4 : " + blobUri);
Log.d(TAG, "Write operation succeeded for SAS " + sas);
response = "success";
//Console.WriteLine();
} catch (StorageException e) {
Log.d(TAG, "Write operation failed for SAS " + sas);
Log.d(TAG, "Additional error information: " + e.getMessage());
response = e.getMessage();
} catch (FileNotFoundException e) {
e.printStackTrace();
response = e.getMessage();
} catch (IOException e) {
e.printStackTrace();
response = e.getMessage();
} catch (URISyntaxException e) {
e.printStackTrace();
response = e.getMessage();
} catch (Exception e){
e.printStackTrace();
response = e.getMessage();
}
现在,当我只上传文字时,它会说我低于错误
服务器未能验证请求。确保 Authorization 标头的值格式正确,包括签名。
现在,我的要求是上传图片文件。因此,当我取消注释上传图像文件的代码时,它没有给我任何错误,甚至没有上传图像文件。
【问题讨论】:
-
请在您的问题中确认 sas
-
@subhashsingh 你在我的问题中确认 sas 是什么意思?我不明白你在说什么。
-
@subhashsingh 你能回答我的问题吗?我不明白你的评论的意思。
-
我认为 AzureMobileStorage 不是 Azure 中的任何术语。您的意思是使用 Azure 移动服务将文件存储在 Azure 存储(通过 sas)中吗?
-
@iDroidExplorer 这里有几个与 SAS 无关的问题。为什么 sas 在你创建的 URL 中需要 toString?您的 sas 令牌是 URL 还是仅仅是令牌?为什么要从 blob URI 创建容器?你能澄清所有这些吗?
标签: android azure azure-storage azure-blob-storage