Android java - azure-storage-blob - blobClient.getProperties().getLastModified() - Wrong number of arguments; expected 1, got 0

93 Views Asked by At

When I try to get the last modified date of a Blobclient using the azure blob storage SDK for java, I get the "Wrong number of arguments" error, saying the expected number is 1, even though the documentation says it takes no arguments and that .getProperties only has optional arguments.

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

The log shows this error: "Azure storage error: Wrong number of arguments; expected 1, got 0"

I have tested to check that the blobClient is found, by logging the name of it and it is found so that is not the problem. I am also using the latest azure blob storage com.azure:azure-storage-blob:12.25.2.

I have also tried .getCreationTime() which throws the same error, so the problem might be with the .getProperties method.

EDIT --------------------------------------------------------------------

I have learned a bit more about the azure storage now and found that the .listBlobs for loop never returns anything or exits.

I can succesfully download a blob client item, but if i try to get the last modified parameter of that blob client i get an error: "Wrong number of arguments; expected 1, got 0".

Here is the complete code:

package com.example.azureblobstoragepropertiestest;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;

import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.models.BlobItem;
import com.azure.storage.blob.BlobClient;

import java.time.OffsetDateTime;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class MainActivity extends Activity {

    private static final String connectStr = "XXXXX";
    private static final String containerName = "XXXXX";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BlobContainerClient containerClient = new BlobServiceClientBuilder()
                .connectionString(connectStr)
                .buildClient()
                .getBlobContainerClient(containerName);

        try {
            String downloadFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();

            BlobClient blobClientDownload = containerClient.getBlobClient("blobtest.txt");

            String filePath = downloadFolder + "/" + blobClientDownload.getBlobName();

            blobClientDownload.downloadToFile(filePath, true);
            System.out.println("Azure blob file downloaded successfully!");

            String fileContent = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
            System.out.println("File Content:\n" + fileContent);

            try {
                OffsetDateTime lastModifiedDownload = blobClientDownload.getProperties().getLastModified();
                System.out.println("Last modified date downloaded blob: " + lastModifiedDownload);
            } catch (Exception e) {
                System.err.println("Get last modified of downloaded blob error: " + e.getMessage());
            }

            System.out.println("Blobs in the container:");
                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);
                }
            System.out.println("No more blobs in the container");

        } catch (Exception e) {
            System.err.println("Azure storage error: " + e.getMessage());
        }
    }
}

here is the output log

2024-03-04 13:26:34.738 19528-19528 System.err              com...zureblobstoragepropertiestest  W  SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
2024-03-04 13:26:34.738 19528-19528 System.err              com...zureblobstoragepropertiestest  W  SLF4J: Defaulting to no-operation (NOP) logger implementation
2024-03-04 13:26:34.738 19528-19528 System.err              com...zureblobstoragepropertiestest  W  SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2024-03-04 13:26:34.752 19528-19528 epropertiestest         com...zureblobstoragepropertiestest  W  Accessing hidden method Ljava/lang/invoke/MethodHandles$Lookup;-><init>(Ljava/lang/Class;)V (max-target-o, reflection, denied)
2024-03-04 13:26:34.865 19528-19528 epropertiestest         com...zureblobstoragepropertiestest  W  Accessing hidden method Lsun/misc/VM;->maxDirectMemory()J (unsupported, reflection, allowed)
2024-03-04 13:26:34.867 19528-19528 PlatformDependent       com...zureblobstoragepropertiestest  I  Your platform does not provide complete low-level API for accessing direct buffers reliably. Unless explicitly requested, heap buffer will always be preferred to avoid potential system instability.
2024-03-04 13:26:34.968 19528-19528 epropertiestest         com...zureblobstoragepropertiestest  W  type=1400 audit(0.0:3344): avc: denied { read } for name="somaxconn" dev="proc" ino=195365 scontext=u:r:untrusted_app:s0:c194,c256,c512,c768 tcontext=u:object_r:proc_net:s0 tclass=file permissive=0 app=com.example.azureblobstoragepropertiestest
2024-03-04 13:26:34.989 19528-19528 epropertiestest         com...zureblobstoragepropertiestest  E  No implementation found for int io.netty.internal.tcnative.Library.aprMajorVersion() (tried Java_io_netty_internal_tcnative_Library_aprMajorVersion and Java_io_netty_internal_tcnative_Library_aprMajorVersion__)
2024-03-04 13:26:35.011 19528-19528 epropertiestest         com...zureblobstoragepropertiestest  E  No implementation found for int io.netty.channel.epoll.Native.offsetofEpollData() (tried Java_io_netty_channel_epoll_Native_offsetofEpollData and Java_io_netty_channel_epoll_Native_offsetofEpollData__)
2024-03-04 13:26:35.015 19528-19528 epropertiestest         com...zureblobstoragepropertiestest  E  No implementation found for int io.netty.channel.kqueue.Native.sizeofKEvent() (tried Java_io_netty_channel_kqueue_Native_sizeofKEvent and Java_io_netty_channel_kqueue_Native_sizeofKEvent__)
2024-03-04 13:26:35.022 19528-19528 epropertiestest         com...zureblobstoragepropertiestest  W  Accessing hidden field Lsun/nio/ch/SelectorImpl;->selectedKeys:Ljava/util/Set; (unsupported, reflection, allowed)
2024-03-04 13:26:35.022 19528-19528 epropertiestest         com...zureblobstoragepropertiestest  W  Accessing hidden field Lsun/nio/ch/SelectorImpl;->publicSelectedKeys:Ljava/util/Set; (unsupported, reflection, allowed)
2024-03-04 13:26:35.029 19528-19528 TrafficStats            com...zureblobstoragepropertiestest  D  tagSocket(130) with statsTag=0xffffffff, statsUid=-1
2024-03-04 13:26:35.031 19528-19528 Compatibil...geReporter com...zureblobstoragepropertiestest  D  Compat change id reported: 170188668; UID 10194; state: ENABLED
2024-03-04 13:26:35.033 19528-19528 MacAddressUtil          com...zureblobstoragepropertiestest  W  Failed to find a usable hardware address from the network interfaces; using random bytes: 6b:dd:5f:a8:87:5b:5e:0c
2024-03-04 13:26:35.346 19528-19528 System.out              com...zureblobstoragepropertiestest  I  Azure blob file downloaded successfully!
2024-03-04 13:26:35.352 19528-19528 System.out              com...zureblobstoragepropertiestest  I  File Content:
2024-03-04 13:26:35.352 19528-19528 System.out              com...zureblobstoragepropertiestest  I  This is a test blob uploaded to azure storage, to check if they can be downloaded.
2024-03-04 13:26:35.393 19528-19528 System.err              com...zureblobstoragepropertiestest  W  Get last modified of downloaded blob error: Wrong number of arguments; expected 1, got 0
2024-03-04 13:26:35.393 19528-19528 System.out              com...zureblobstoragepropertiestest  I  Blobs in the container:
2024-03-04 13:26:54.920 19528-19538 epropertiestest         com...zureblobstoragepropertiestest  I  Background concurrent copying GC freed 160645(16MB) AllocSpace objects, 16(336KB) LOS objects, 49% free, 9573KB/18MB, paused 197us,39us total 114.732ms
2024-03-04 13:26:54.923 19528-19540 System                  com...zureblobstoragepropertiestest  W  A resource failed to call close. 
2024-03-04 13:27:38.643 19528-19542 epropertiestest         com...zureblobstoragepropertiestest  I  Explicit concurrent copying GC freed 805(67KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 9574KB/18MB, paused 61us,40us total 70.543ms

here is a snippet from the azure portal, showing that the calls do go through. Azure activity.

1

There are 1 best solutions below

9
Venkatesan On

To get the last modified date of a Blobclient using the Azure blob storage SDK for java

You can use the below code to get the last modified date and time in the container using Azure Java SDK.

Code:

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

The above code uses the Azure Storage Blob Java SDK to connect to a storage account using a connection string and retrieve a container client. It then lists all the blobs in the container along with their last modified date. If any error occurs during the process, it prints an error message.

Output:

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

enter image description here

Dependency:

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-storage-blob</artifactId>
    <version>12.25.2</version>
</dependency>

Reference: Quickstart: Azure Blob Storage library - Java | Microsoft Learn