Java azure 容器通过 SAS 令牌连接

问题描述 投票:0回答:1

我正在关注本教程https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blob-java-get-started?tabs=sas-token#authorize-access-and- connect-to-blob-storage 使用生成的令牌来访问 azure 容器中的文件。

作为依赖项,我只使用:

implementation 'com.azure:azure-storage-blob:12.14.2'

我的连接代码如下:

BlobContainerClient container = new BlobContainerClientBuilder()
                .endpoint("https://mycontainername.blob.core.windows.net/subcontainer")
                .sasToken("sv=2020-10-02&st=2024-03-11T13:58:42Z&se=2024-03-12T14:03:42Z&sr=c&sp=racwl&sig=HVoBAVRrDWaEY1KDr0oxUCFXS8R63L%2B7S5X8n457LTA%3D")
                .buildClient();

如果我完全使用这个字符串(如

<endpoint>?<sasToken>
)并在浏览器中附加
&restype=container&comp=list
,我会得到此子容器中所有元素的表示。

但是在代码中我无法让它工作。一旦我调用类似

container.listBlobs().stream().count()
之类的东西,那么任何需要真正使用连接的东西,我都会得到
BlobStorageException
:

Status code 403, "<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:f8b4f3b8-001e-0042-154c-74c44a000000
Time:2024-03-12T07:13:39.0826318Z</Message><AuthenticationErrorDetail>Signature did not match. String to sign used was racwl
2024-03-11T13:58:42Z
2024-03-12T14:03:42Z
/blob/mycontainername/subcontainer



2020-10-02
c





</AuthenticationErrorDetail></Error>"

错误细节看起来很混乱,是否可能存在一些编码/解码问题?

当我从令牌中读取时,权限应该不是问题,因为

sr=c
(资源=容器),
sp=racwl
(权限=读取、访问、创建、写入、列表),对吗?

java azure sas
1个回答
0
投票

状态代码 403:“

AuthenticationFailed
服务器无法验证请求。请确保授权标头的值格式正确,包括签名。

当您没有访问存储帐户的适当权限或传递错误的 SAS 令牌时,会出现上述错误。

在您的代码中,SAS 令牌中缺少

?

这是获取斑点计数的更正代码。

代码:

import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobContainerClientBuilder;

public class App {
    public static void main(String[] args) {
        BlobContainerClient containerClient = new BlobContainerClientBuilder()
                .endpoint("https://venkat789.blob.core.windows.net/test")
                .sasToken("?sp=racwl&st=2024-03-12T07:39:44Z&se=2024-03-12T15:39:44Z&spr=https&sv=2022-11-02&sr=c&sig=Oxxxxx")
                .buildClient();
        long blobCount = containerClient.listBlobs().stream().count();
        System.out.println(blobCount); 
    }
}

输出:

11

Corrected image

© www.soinside.com 2019 - 2024. All rights reserved.