使用java在Android中使用SAS URL将文件上传到Azure存储中

发布于 2025-02-13 12:36:02 字数 2777 浏览 1 评论 0 原文

我正在尝试直接从Android应用程序中使用SAS URL上传文件。但是,当我尝试上传时,它会给我发送以下存储Exception错误:请求的URI不代表服务器上的任何资源。

在这里,我的SAS URL就是这样: https://baseurl.com/images/posts/test123.jpg?sv = 27-11-09& sr = b& amp; st = 2022-07- 04:26:3z& se = 2022-07-05T04:31:33z& sp = racwd&spr = https& sig = ftxffmetam52nlbzqh

这是我正在尝试的代码:

    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;
    }

I am trying to upload a file using sas url in azure storage directly from my android app. But when I am trying to upload, it sends me the following StorageException error: The requested URI does not represent any resource on the server.

Here my sas url is like this:
https://baseurl.com/images/posts/test123.jpg?sv=27-11-09&sr=b&st=2022-07-04:26:3Z&se=2022-07-05T04:31:33Z&sp=racwd&spr=https&sig=fTxFFmeTam52nlbZQH

Here is the code that I am trying:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想挽留 2025-02-20 12:36:02

这是解决方案:

 val sasUrl = resource.data.data //get this sas url from api or anywhere you want
   try {
     val imageStream = contentResolver.openInputStream(Uri.fromFile(File(selectionResult!![0])))
     val imageLength = imageStream!!.available()
     val handler = Handler(Looper.getMainLooper())
     val th = Thread {
         try {
            AzureManager.UploadImage(sasUrl,imageStream, imageLength)
         } catch (ex: Exception) {
            val exceptionMessage = ex.message
            Log.e("SAS_ERROR", "error1: ${ex.localizedMessage}")
         }
       }
     }
             th.start()
      } catch (ex: Exception) {
             Log.e("SAS_ERROR", "error2: ${ex.localizedMessage}")
             Toast.makeText(this, ex.message, Toast.LENGTH_SHORT).show()
      }










//In AzureManager class create this method
    public static void UploadImage(String sasUrl, InputStream image, int imageLength) throws Exception {
        CloudBlockBlob cloudBlockBlob = new CloudBlockBlob(new URI(sasUrl));
        cloudBlockBlob.upload(image, imageLength);
    }

Here is the solution:

 val sasUrl = resource.data.data //get this sas url from api or anywhere you want
   try {
     val imageStream = contentResolver.openInputStream(Uri.fromFile(File(selectionResult!![0])))
     val imageLength = imageStream!!.available()
     val handler = Handler(Looper.getMainLooper())
     val th = Thread {
         try {
            AzureManager.UploadImage(sasUrl,imageStream, imageLength)
         } catch (ex: Exception) {
            val exceptionMessage = ex.message
            Log.e("SAS_ERROR", "error1: ${ex.localizedMessage}")
         }
       }
     }
             th.start()
      } catch (ex: Exception) {
             Log.e("SAS_ERROR", "error2: ${ex.localizedMessage}")
             Toast.makeText(this, ex.message, Toast.LENGTH_SHORT).show()
      }










//In AzureManager class create this method
    public static void UploadImage(String sasUrl, InputStream image, int imageLength) throws Exception {
        CloudBlockBlob cloudBlockBlob = new CloudBlockBlob(new URI(sasUrl));
        cloudBlockBlob.upload(image, imageLength);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文