Android 使用SAS(共享访问签名)使用Azure存储上载文件
我知道有一个库可用于使用Azure存储上载文件。我也有同样的想法 但是,他们没有给出如何使用SAS的信息。我有帐户名和sas url用于访问和上传文件。但我不知道如何使用它来上传文件 如果我使用上述库,它会显示无效的存储连接字符串,因为我没有在其中传递密钥(sas不需要)。所以我很困惑如何上传文件 我还参考了使用sas上传文件的文档。但是没有采取适当的步骤来做到这一点。他们已经为他们的windows应用程序制作了演示。我想在安卓系统中使用sas 更新 我已经尝试使用下面的代码来检查和访问SASAndroid 使用SAS(共享访问签名)使用Azure存储上载文件,android,azure,azure-storage,azure-mobile-services,azure-storage-blobs,Android,Azure,Azure Storage,Azure Mobile Services,Azure Storage Blobs,我知道有一个库可用于使用Azure存储上载文件。我也有同样的想法 但是,他们没有给出如何使用SAS的信息。我有帐户名和sas url用于访问和上传文件。但我不知道如何使用它来上传文件 如果我使用上述库,它会显示无效的存储连接字符串,因为我没有在其中传递密钥(sas不需要)。所以我很困惑如何上传文件 我还参考了使用sas上传文件的文档。但是没有采取适当的步骤来做到这一点。他们已经为他们的windows应用程序制作了演示。我想在安卓系统中使用sas 更新 我已经尝试使用下面的代码来检查和访问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();
}
现在,当我只上传文本时,它会告诉我下面的错误
服务器无法对请求进行身份验证。确保包括签名在内的授权标头的值格式正确
现在,我的要求是上传图像文件。因此,当我取消对上载图像文件的代码的注释时,它不会给我任何错误,甚至不会上载图像文件。将图片上载到
BLOB
存储中。我找了几个小时才找到的。看一看:-
上载照片图像是一个多步骤过程:
首先拍摄一张照片,并将TodoItem
行插入包含Azure存储使用的新元数据字段的SQL数据库。
一个新的移动服务SQL insert脚本向Azure存储请求共享访问签名(SAS)
。
该脚本将blob的SAS和URI返回给客户端。
客户端使用SAS和blob URI上传照片
那么什么是SAS
?
在客户端应用程序中存储将数据上载到Azure存储服务所需的凭据是不安全的。相反,您将这些凭据存储在移动服务中,并使用它们生成共享访问签名(SAS)
,该签名授予上载新图像的权限。SAS
,一个过期5分钟的凭证,由移动服务安全地返回到客户端应用程序。然后,应用程序使用此临时凭据上载图像
用于进一步查询和详细分析。访问此官方文档@kumar kundal
你所解释的机制是完全正确的。
下面是关于将配置文件映像上载到Azure服务器的更详细答案
首先创建SAS url以将图像(或任何文件)上载到blob存储:
String sasUrl = "";
// mClient is the MobileServiceClient
ListenableFuture<JsonElement> result = mClient.invokeApi(SOME_URL_CREATED_TO_MAKE_SAS, null, "GET", null);
Futures.addCallback(result, new FutureCallback<JsonElement>() {
@Override
public void onSuccess(JsonElement result) {
// here you will get SAS url from server
sasUrl = result; // You need to parse it as per your response
}
@Override
public void onFailure(Throwable t) {
}
});
现在,您需要将sas url与上载url颠倒。请参阅下面的代码,其中我已将SAS url与上载请求一起翻转
try {
File source = new File(filePath); // File path
String extantion = source.getAbsolutePath().substring(source.getAbsolutePath().lastIndexOf("."));
// create unique number to identify the image/file.
// you can also specify some name to image/file
String uniqueID = "image_"+ UUID.randomUUID().toString().replace("-", "")+extantion;
String blobUri = MY_URL_TO_UPLOAD_PROFILE_IMAGE + sas.replaceAll("\"","");
StorageUri storage = new StorageUri(URI.create(blobUri));
CloudBlobClient blobCLient = new CloudBlobClient(storage);
CloudBlobContainer container = blobCLient.getContainerReference("");
CloudBlockBlob blob = container.getBlockBlobReference(uniqueID);
BlobOutputStream blobOutputStream = blob.openOutputStream();
byte[] buffer = fileToByteConverter(source);
ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
int next = inputStream.read();
while (next != -1) {
blobOutputStream.write(next);
next = inputStream.read();
}
blobOutputStream.close();
// YOUR IMAGE/FILE GET UPLOADED HERE
// IF YOU HAVE FOLLOW DOCUMENT, YOU WILL RECEIVE IMAGE/FILE URL HERE
} catch (StorageException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
我希望这些信息能帮助您使用blob存储上传文件。
除此之外,如果您还有任何疑问,请告诉我。我可以在这方面提供帮助。请在您的电子邮件中确认sasquestion@subhashsingh你在我的问题中所说的确认sas是什么意思?“我不明白你在说什么。”苏巴什辛格,你能回答我的问题吗?我不明白你的评论的意思。我认为AzureMobileStorage不是Azure中的任何术语。您的意思是使用Azure移动服务(通过sas)将文件存储在Azure存储中吗?@iDroidExplorer您遇到了几个与sas无关的问题。为什么sas需要插入您创建的URL?您的sas令牌是URL还是仅仅是令牌?为什么要用BlobURI创建一个容器?你能澄清所有这些吗?请帮我解决这个问题。。如何使用单个类序列化多个表
try {
File source = new File(filePath); // File path
String extantion = source.getAbsolutePath().substring(source.getAbsolutePath().lastIndexOf("."));
// create unique number to identify the image/file.
// you can also specify some name to image/file
String uniqueID = "image_"+ UUID.randomUUID().toString().replace("-", "")+extantion;
String blobUri = MY_URL_TO_UPLOAD_PROFILE_IMAGE + sas.replaceAll("\"","");
StorageUri storage = new StorageUri(URI.create(blobUri));
CloudBlobClient blobCLient = new CloudBlobClient(storage);
CloudBlobContainer container = blobCLient.getContainerReference("");
CloudBlockBlob blob = container.getBlockBlobReference(uniqueID);
BlobOutputStream blobOutputStream = blob.openOutputStream();
byte[] buffer = fileToByteConverter(source);
ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
int next = inputStream.read();
while (next != -1) {
blobOutputStream.write(next);
next = inputStream.read();
}
blobOutputStream.close();
// YOUR IMAGE/FILE GET UPLOADED HERE
// IF YOU HAVE FOLLOW DOCUMENT, YOU WILL RECEIVE IMAGE/FILE URL HERE
} catch (StorageException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}