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

2023-09-06 04:07:32 作者:爱到妥协最后还是无解-

我知道,有上传使用Azure存储文件库中。我指 这 的一样。

不过,他们没有提供资料,说明如何使用SAS这一点。我的帐户名和SAS的URL访问和上传文件存在。但我不知道如何使用,对上传的文件。

如果我使用上面提到的库就说明我invalide存储连接字符串,因为我没有通过它的密钥(不需要使用SAS)。所以我有点迷惑了我如何可以上传文件。

我有指 这 文档还使用SAS上传文件。但没有得到适当的措施来做到这一点。他们已经为他们的Windows应用程序做演示。我想有一个在安卓与SAS使用。

谁能帮我在这?

更新

我尝试用以下code参照 控制台应用程序 检查和访问SAS。

 尝试{
        //尝试执行容器操作与提供的SAS。

        //返回一个参考使用SAS URI中的容器。
        // CloudBlockBlob斑点=新CloudBlockBlob(新StorageUri(新的URI(SAS)));
        的String []海峡= userId.split(:);
        串blobUri =htt​​ps://myStorageAccountName.blob.core.windows.net/image/+海峡[1] +/故事/+ storyId +/image1.jpg+ sas.toString()。的replaceAll( \,);
        Log.d(TAG,结果:: blobUrl 1:+ blobUri);
        CloudBlobContainer集装箱=新CloudBlobContainer(新的URI(blobUri));
        Log.d(TAG,结果:: blobUrl 2:+ blobUri);
        CloudBlockBlob斑点= container.getBlockBlobReference(image1.jpg);
        。字符串文件路径= postData.get(0).getUrl()的toString();
        / *文件源=新的文件(getRealPathFromURI(getApplicationContext(),Uri.parse(文件路径))); // 文件路径
        blob.upload(新的FileInputStream(源),source.length()); * /
        Log.d(TAG,结果:: blobUrl 3:+ blobUri);
        //blob.upload(new的FileInputStream(源),source.length());
        //blob.uploadText("Hello这是测试...); //上传文本文件
        Log.d(TAG,结果:: blobUrl 4:+ blobUri);
        Log.d(TAG,写操作成功的SAS+ SAS);
        反应=成功;
        //Console.WriteLine();
    }赶上(StorageException E){
        Log.d(TAG,写操作失败SAS+ SAS);
        Log.d(TAG,其他错误信息:+ e.getMessage());
        响应= e.getMessage();
    }赶上(FileNotFoundException异常E){
        e.printStackTrace();
        响应= e.getMessage();
    }赶上(IOException异常E){
        e.printStackTrace();
        响应= e.getMessage();
    }赶上(的URISyntaxException E){
        e.printStackTrace();
        响应= e.getMessage();
    }赶上(例外五){
        e.printStackTrace();
        响应= e.getMessage();
    }
 

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

  

服务器无法进行身份验证的要求。确保授权头的值形成正确包括签名。

三十二. 利用 AzCopy来对Azure Blob Storage中的数据进行复制迁移

现在,我的要求是,以上传图像文件。所以,当我取消code上传图像文件时,它不会给我任何错误,但即使没有上传图像文件。我不知道什么是错在这里。任何人都可以帮助我在这里。

感谢。

解决方案

@kumar kundal 你必须解释其机理是完全正确的。 下面是关于上传个人资料图片,以在Azure服务器的云纹详细的答案。

首先,创建SAS网址上传图片(或文件)到Blob存储:

 字符串sasUrl =;
// mClient是MobileServiceClient
ListenableFuture< JsonElement>结果= mClient.invokeApi(SOME_URL_CREATED_TO_MAKE_SAS,空,GET,NULL);
Futures.addCallback(因此,新FutureCallback< JsonElement>(){
        @覆盖
        公共无效的onSuccess(JsonElement结果){
            //在这里你会得到的SAS URL从服务器
            sasUrl =结果; //你需要分析它根据您的回复
        }

        @覆盖
        公共无效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。见下面code中,我有颠覆的SAS网址我上传的请求。

 尝试{
        文件源=新的文件(文件路径); // 文件路径
        串extantion = source.getAbsolutePath()子(source.getAbsolutePath()lastIndexOf()。。)。
        //创建唯一编号来识别图像/文件。
        //你也可以指定一些名称,图像/文件
        字符串UNIQUEID =图片_+ UUID.randomUUID()的toString()代替( - ,)+ extantion。;
        串blobUri = MY_URL_TO_UPLOAD_PROFILE_IMAGE + sas.replaceAll(\,);
        StorageUri存储=新StorageUri(URI.create(blobUri));
        CloudBlobClient blobCLient =新CloudBlobClient(储存);
        CloudBlobContainer容器= blobCLient.getContainerReference();
        CloudBlockBlob BLOB = container.getBlockBlobReference(UNIQUEID);
        BlobOutputStream blobOutputStream = blob.openOutputStream();
        byte []的缓冲区= fileToByteConverter(源);
        ByteArrayInputStream进行的InputStream =新ByteArrayInputStream的(缓冲);
        INT下一= inputStream.read();
        而(下!= -1){
            blobOutputStream.write(下);
            接下来= inputStream.read();
        }
        blobOutputStream.close();
        //图像/文件要上载这里
        //如果你有FOLLOW文档,您将在这里接收图像/文件的URL
    }赶上(StorageException E){
        e.printStackTrace();
    }赶上(FileNotFoundException异常E){
        e.printStackTrace();
    }赶上(例外五){
        e.printStackTrace();
    }
 

我希望这些信息有助于您大量上载使用Blob存储的文件。 请让我知道如果您有任何疑问,除了这个。我可以帮助这一点。

I know that there is library available for uploading the file using Azure Storage. I have refer this for same.

But, They have not give information for how to use SAS with that. I have account name, and sas url for access and upload file there. But i don't know how to use that for uploading file.

If i use above mention library it shows me invalide storage connection string because i am not passing the key in it (Which is not required with sas). So i am little confuse that How i can upload file.

I have refer this documentation also for uploading file using sas. but not getting proper steps to do this. They have made demo for their windows app. I want to have that in android with use of sas.

Can anyone help me in this?

UPDATE

I have try with below code with reference to the console app made by Azure to check and access 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();
    }

Now, when i upload text only it says me below error

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

Now, My requirement is to upload Image file. So when i uncomment code for uploading Image file it is not giving me any error but even not uploading image file. I don't know whats wrong here. Can anyone help me here.

Thanks.

解决方案

@kumar kundal The mechanism that you have explain is completely right. Below is the moire detail answer about uploading profile image to the Azure Server.

First create SAS url to upload Image(or any file) to blob storage:

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

Now, you have sasURL with you. That will be something like below string:

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

Now, you need to upend the sas url with your uploading url. See below code in which I have upend the SAS url with my uploading request.

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

I hope this information help you lot for uploading the file using blob storage. Please let me know if you have any doubt apart from this. I can help in that.