【问题标题】:Upload file with Azure Storage using SAS (Shared Access Signature)使用 SAS(共享访问签名)通过 Azure 存储上传文件
【发布时间】:2015-10-29 02:10:28
【问题描述】:

我知道有可用于使用 Azure 存储上传文件的库。我已经参考了this

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

如果我使用上面提到的库,它会显示无效的存储连接字符串,因为我没有在其中传递密钥(sas 不需要)。所以我很困惑如何上传文件。

我也参考了this 文档以使用 sas 上传文件。但没有得到适当的步骤来做到这一点。他们已经为他们的 windows 应用程序制作了演示。我想在 android 中使用 sas。

更新

我参考console app made by 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();
    }

现在,当我只上传文字时,它会说我低于错误

服务器未能验证请求。确保 Authorization 标头的值格式正确,包括签名。

现在,我的要求是上传图片文件。因此,当我取消注释上传图像文件的代码时,它没有给我任何错误,甚至没有上传图像文件。

【问题讨论】:

  • 请在您的问题中确认 sas
  • @subhashsingh 你在我的问题中确认 sas 是什么意思?我不明白你在说什么。
  • @subhashsingh 你能回答我的问题吗?我不明白你的评论的意思。
  • 我认为 AzureMobileStorage 不是 Azure 中的任何术语。您的意思是使用 Azure 移动服务将文件存储在 Azure 存储(通过 sas)中吗?
  • @iDroidExplorer 这里有几个与 SAS 无关的问题。为什么 sas 在你创建的 URL 中需要 toString?您的 sas 令牌是 URL 还是仅仅是令牌?为什么要从 blob URI 创建容器?你能澄清所有这些吗?

标签: android azure azure-storage azure-blob-storage


【解决方案1】:

@kumar 昆达尔 你解释的机制是完全正确的。 下面是关于上传配置文件图片到 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) {
        }
    });

现在,您有了 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 网址附加到您的上传网址。请参阅下面的代码,其中我在上传请求中附加了 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();
    }

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

【讨论】:

  • 请帮我解决这个问题..如何使用单个类对多个表进行序列化
【解决方案2】:

将图片上传到BLOB 存储。我在搜索了几个小时后得到了它。看看:-

上传照片图像是一个多步骤的过程:

首先,您拍摄一张照片,并将 TodoItem 行插入包含 Azure 存储使用的新元数据字段的 SQL 数据库。 新的移动服务 SQL 插入脚本向 Azure 存储请求 Shared Access Signature (SAS)。 该脚本将 Blob 的 SAS 和 URI 返回给客户端。 客户端使用 SAS 和 blob URI 上传照片。

那么SAS 是什么?

在客户端应用程序中存储将数据上传到 Azure 存储服务所需的凭据是不安全的。相反,您将这些凭据存储在您的移动服务中,并使用它们生成一个Shared Access Signature (SAS),该Shared Access Signature (SAS) 授予上传新图像的权限。 SAS 是一个有效期为 5 分钟的凭证,它由移动服务安全地返回给客户端应用程序。然后,该应用使用此临时凭据上传图像。

用于进一步查询和详细分析。访问本官方文档https://azure.microsoft.com/en-us/documentation/articles/mobile-services-android-upload-data-blob-storage/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    • 2020-05-09
    • 2022-01-07
    • 2013-10-18
    • 2016-04-16
    • 2016-12-10
    相关资源
    最近更新 更多