繁体   English   中英

使用Java在android中使用sas url将文件上传到azure存储

[英]Upload file into azure storage using sas url in android using Java

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

这里我的 sas 网址是这样的: 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

这是我正在尝试的代码:

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

这是解决方案:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM