当我尝试使用适用于 Java 的 azure blob 存储 SDK 获取 Blob 客户端的最后修改日期时,我收到“参数数量错误”错误,指出预期数量为 1,即使文档说它不带参数并且
.getProperties
只有可选参数。
BlobContainerClient containerClient = new BlobServiceClientBuilder()
.connectionString(connection)
.buildClient()
.getBlobContainerClient(containerName);
String sasToken = createContainerSasToken(containerClient);
BlobClient blobClient = new BlobClientBuilder()
.endpoint(endPoint)
.sasToken(sasToken)
.buildClient();
try {
OffsetDateTime lastModified = blobClient.getProperties().getCreationTime();
Log.i("Azure storage", "Last modified: " + lastModified);
} catch (Exception e) {
Log.e("Azure storage", "Azure storage error: " + e.getMessage());
}
日志显示此错误:“Azure 存储错误:参数数量错误;预期为 1,实际为 0”
我已经测试过,通过记录 blobClient 的名称来检查是否找到它,并且找到了它,所以这不是问题。我还使用最新的 azure blob 存储 com.azure:azure-storage-blob:12.25.2.
我也尝试过
.getCreationTime()
,它会引发相同的错误,所以问题可能出在 .getProperties
方法上。
使用适用于 java 的 Azure Blob 存储 SDK 获取 Blob 客户端的上次修改日期
您可以使用以下代码通过 Azure Java SDK 获取容器中的最后修改日期和时间。
代码:
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.models.BlobItem;
import java.time.OffsetDateTime;
public class App {
public static void main(String[] args) {
String connectStr = "xxxxx";
String containerName = "xxxx";
BlobContainerClient containerClient = new BlobServiceClientBuilder()
.connectionString(connectStr)
.buildClient()
.getBlobContainerClient(containerName);
try {
for (BlobItem blobItem : containerClient.listBlobs()) {
BlobClient blobClient = containerClient.getBlobClient(blobItem.getName());
OffsetDateTime lastModified = blobClient.getProperties().getLastModified();
System.out.println("Blob: " + blobItem.getName() + ", Last modified: " + lastModified);
}
} catch (Exception e) {
System.err.println("Azure storage error: " + e.getMessage());
}
}
}
以上代码使用 Azure Storage Blob Java SDK 使用连接字符串连接到存储帐户并检索容器客户端。然后它会列出容器中的所有 Blob 及其
last modified date
。如果过程中发生任何错误,它会打印一条错误消息。
输出:
Blob: currency.csv, Last modified: 2024-01-19T11:18:59Z
Blob: demo.mp4, Last modified: 2024-02-21T05:16:48Z
Blob: sample-file-sd.mp4, Last modified: 2024-02-21T06:55:34Z
依赖:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>12.25.2</version>
</dependency>