使用SAS(共享访问签名)使用Azure存储上载文件

Shr*_*jan 8 android azure azure-storage azure-storage-blobs azure-mobile-services

我知道有可用于使用Azure存储上传文件的库.我也是这样说的.

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

如果我使用上面提到的库它显示我的invalide存储连接字符串,因为我没有传递密钥(sas不需要).所以我很难混淆我如何上传文件.

我也参考了这个文档,用于使用sas上传文件.但没有采取适当的措施来做到这一点.他们为他们的Windows应用程序制作了演示.我希望在android中使用sas.

任何人都可以帮助我吗?

UPDATE

我已尝试使用以下代码参考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();
    }
Run Code Online (Sandbox Code Playgroud)

现在,当我上传文字时,它会说我下面的错误

服务器无法验证请求.确保正确形成Authorization标头的值,包括签名.

现在,我的要求是上传图像文件.因此,当我取消注释上传图像文件的代码时,它不会给我任何错误,但即使不上传图像文件.我不知道这里有什么不对.有人能帮我一下吗.

谢谢.

Shr*_*jan 3

@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) {
        }
    });
Run Code Online (Sandbox Code Playgroud)

现在,您已经有了 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
Run Code Online (Sandbox Code Playgroud)

现在,您需要将 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();
    }
Run Code Online (Sandbox Code Playgroud)

我希望这些信息对您使用 blob 存储上传文件有很大帮助。除此之外,如果您还有任何疑问,请告诉我。我可以帮忙。