gpt4 book ai didi

android - 使用 SAS(共享访问签名)通过 Azure 存储上传文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:58:00 24 4
gpt4 key购买 nike

我知道有一个库可用于使用 Azure 存储上传文件。我已经引用this同样的。

但是,他们没有提供如何使用 SAS 的信息。我有帐户名和用于访问和上传文件的 sas url。但我不知道如何使用它来上传文件。

如果我使用上面提到的库,它会显示无效的存储连接字符串,因为我没有在其中传递 key (这对于 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();
}

现在,当我仅上传文本时,它会显示以下错误

Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

现在,我的要求是上传图像文件。因此,当我取消注释上传图像文件的代码时,它不会给我任何错误,甚至不会上传图像文件。

最佳答案

@库马尔·昆达尔您所解释的机制是完全正确的。以下是有关将个人资料图像上传到 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) {
}
});

现在,您已经有了 sasURL。这将类似于下面的字符串:

sv=2015-04-05&ss=bf&srt=s&st=2015-04-29T22%3A18%3A26Z&se=2015-04-30T02%3A23%3A26Z&sr=b&sp=rw&sip=168.1.5.60-168.1.5.70&spr=https&sig=F%6GRVAZ5Cdj2Pw4tgU7IlSTkWgn7bUkkAg8P6HESXwmf%4B

现在,您需要将 sas url 附加到您的上传 url。请参阅下面的代码,其中我已在上传请求中附加了 SAS 网址。

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 存储上传文件有帮助。除此之外,如果您还有任何疑问,请告诉我。我可以帮忙。

关于android - 使用 SAS(共享访问签名)通过 Azure 存储上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31847102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com