我知道有一个库可以使用Azure Storage上传文件。我参考了这个,但他们没有提供如何使用SAS的信息。我有账户名称和用于访问和上传文件的SAS URL。但我不知道如何使用它来上传文件。如果我使用上述库,它会显示无效的存储连接字符串,因为我没有在其中传递密钥(使用SAS不需要密钥)。所以我很困惑如何上传文件。我还查阅了这篇文档,想要使用SAS上传文件。但是,我没有找到正确的步骤。他们为其Windows应用程序制作了演示文稿,我希望能在Android上使用SAS实现相同的功能。
更新:参考Azure制作的控制台应用程序,我尝试了下面的代码以检查和访问SAS。
更新:参考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标头的值正确形成,包括签名。
现在,我的要求是上传图像文件。因此,当我取消上传图像文件的代码注释时,它不会给我任何错误,但也不会上传图像文件。