使用java在Android中使用SAS URL将文件上传到Azure存储中
我正在尝试直接从Android应用程序中使用SAS URL上传文件。但是,当我尝试上传时,它会给我发送以下存储Exception错误:请求的URI不代表服务器上的任何资源。
这是我正在尝试的代码:
public static void uploadImageUsingSasUrl(String sasUrl, String imagePath) {
try {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Log.e("SAS_UPLOAD", "sas_url: "+sasUrl);
File source = new File(imagePath); // File path
String uniqueID = "test123.jpg"; // now added as hard coded just for testing
StorageUri storage = new StorageUri(URI.create(sasUrl));
CloudBlobClient blobCLient = new CloudBlobClient(storage);
CloudBlobContainer container = blobCLient.getContainerReference("images/posts");
container.createIfNotExists();
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();
Log.e("SAS_UPLOAD", "error1: "+e.getLocalizedMessage());
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e("SAS_UPLOAD", "error2: "+e.getLocalizedMessage());
} catch (Exception e){
e.printStackTrace();
Log.e("SAS_UPLOAD", "error3: "+e);
}
}
public static byte[] fileToByteConverter(File file)
{
byte[] fileBytes = new byte[(int) file.length()];
try(FileInputStream inputStream = new FileInputStream(file))
{
inputStream.read(fileBytes);
}
catch (Exception ex)
{
ex.printStackTrace();
}
return fileBytes;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是解决方案:
Here is the solution: