繁体   English   中英

使用 spring boot 在 azure 中从 v8 升级到 v12 java sdk 后无法连接到 azure-blob-storage

[英]Unable to connect to azure-blob-storage after upgrading from v8 to v12 java sdk in azure using spring boot

我正在将使用 azure-blob-storage 的现有 Spring Boot 应用程序从 V8 SDK 升级到 V12 SDK。 但我收到授权错误。 我几乎尝试了 azure sdk 中建议的所有示例,但它们都不起作用。 下面是 v8 和 v12 之间的代码。

V8(工作正常):

String endPoint = https://XXXXXXX.blob.core.windows.net/ecommerce/
String sasToken = sp=racwl&st=2021-06-01T05:12:04Z&se=2026-06-01T13:12:04Z&spr=https&sv=2020-02-05&sr=c&sig=XXXXXX%2BXXXXXXXXXXX%2BVVVVVVVVVVVV%3D
StorageCredentialsSharedAccessSignature s = new StorageCredentialsSharedAccessSignature("sas_token");
CloudBlobContainer cbc = new CloudBlobContainer(s.transformUri(new URI(endPoint)));

CloudBlobDirectory bd = cbc.getDirectoryReference("container_name");
InputStream is = new ByteArrayInputStream("my_string".getBytes());
CloudBlockBlob cbb = bd.getBlockBlobReference("blob_name");
cbb.upload(is, "my_string".length());

V12(身份验证失败):


String endPoint = https://XXXXXXX.blob.core.windows.net/ecommerce/
String sasToken = sp=racwl&st=2021-06-01T05:12:04Z&se=2026-06-01T13:12:04Z&spr=https&sv=2020-02-05&sr=c&sig=XXXXXX%2BXXXXXXXXXXX%2BVVVVVVVVVVVV%3D
BlobContainerClient bc = new BlobContainerClientBuilder().endpoint(endPoint).sasToken(sasToken).containerName("container_name").buildClient();
InputStream targetStream = new ByteArrayInputStream("my_string".getBytes());
BlockBlobClient cbb = bc.getBlobClient("blob_name").getBlockBlobClient();
cbb.upload(targetStream, payload.length()); ----> This is where it is throwing the exception

V12 方法在身份验证失败时出错:

com.azure.storage.blob.models.BlobStorageException: If you are using a StorageSharedKeyCredential, and the server returned an error message that says 'Signature did not match', you can compare the string to sign with the one generated by the SDK. To log the string to sign, pass in the context key value pair 'Azure-Storage-Log-String-To-Sign': true to the appropriate method call.
If you are using a SAS token, and the server returned an error message that says 'Signature did not match', you can compare the string to sign with the one generated by the SDK. To log the string to sign, pass in the context key value pair 'Azure-Storage-Log-String-To-Sign': true to the appropriate generateSas method call.
Please remember to disable 'Azure-Storage-Log-String-To-Sign' before going to production as this string can potentially contain PII.
Status code 403, (empty body)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    ...

我现在可以使用以下代码成功连接。 但是,执行器/健康页面仍然存在问题

BlobContainerClient bcc = new BlobContainerClientBuilder().endpoint(reportProperties.getEndpoint() + "/" + containerName + "/" + "?" + reportProperties.getSastoken()).buildClient();
BlobClient blobClient = bcc.getBlobClient(blobName);
InputStream is = new ByteArrayInputStream("my_string".getBytes());
blobClient.upload(is, payload.length());

经过一天的工作,这就是我设法让它与 SDK V12.14.1 一起使用的方法:

    String endpoint = String.format(Locale.ROOT,
            "https://%s.blob.core.windows.net", "myStorage");
    
    AzureSasCredential sasCredential = new AzureSasCredential(
            "sp=racwdl&st=2021-10-21T12:23:00Z&se=2021-10-21T20:23:00Z&spr=https&sv=2020-08-04&sr=c&sig=vV...MI%3D");

    BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
            .endpoint(endpoint).credential(sasCredential).buildClient();

    BlobContainerClient blobContainerClient = blobServiceClient
            .getBlobContainerClient("myContainer");
    for (BlobItem blobItem : blobContainerClient.listBlobs()) {
        BlockBlobClient blobClient = blobContainerClient
                .getBlobClient(blobItem.getName()).getBlockBlobClient();
        System.out.println(blobClient.getBlobName());
    }

暂无
暂无

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

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