How can I tell how many objects I've stored in an S3 bucket?

后端 未结 29 3520
逝去的感伤
逝去的感伤 2020-12-02 04:45

Unless I\'m missing something, it seems that none of the APIs I\'ve looked at will tell you how many objects are in an /. Is ther

相关标签:
29条回答
  • 2020-12-02 05:09

    Although this is an old question, and feedback was provided in 2015, right now it's much simpler, as S3 Web Console has enabled a "Get Size" option:

    Which provides the following:

    0 讨论(0)
  • 2020-12-02 05:10

    Following is how you can do it using java client.

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-s3</artifactId>
        <version>1.11.519</version>
    </dependency>
    
    import com.amazonaws.ClientConfiguration;
    import com.amazonaws.Protocol;
    import com.amazonaws.auth.AWSStaticCredentialsProvider;
    import com.amazonaws.auth.BasicAWSCredentials;
    import com.amazonaws.services.s3.AmazonS3;
    import com.amazonaws.services.s3.AmazonS3ClientBuilder;
    import com.amazonaws.services.s3.model.ObjectListing;
    
    public class AmazonS3Service {
    
        private static final String S3_ACCESS_KEY_ID = "ACCESS_KEY";
        private static final String S3_SECRET_KEY = "SECRET_KEY";
        private static final String S3_ENDPOINT = "S3_URL";
    
        private AmazonS3 amazonS3;
    
        public AmazonS3Service() {
            ClientConfiguration clientConfiguration = new ClientConfiguration();
            clientConfiguration.setProtocol(Protocol.HTTPS);
            clientConfiguration.setSignerOverride("S3SignerType");
            BasicAWSCredentials credentials = new BasicAWSCredentials(S3_ACCESS_KEY_ID, S3_SECRET_KEY);
            AWSStaticCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials);
            AmazonS3ClientBuilder.EndpointConfiguration endpointConfiguration = new AmazonS3ClientBuilder.EndpointConfiguration(S3_ENDPOINT, null);
            amazonS3 = AmazonS3ClientBuilder.standard().withCredentials(credentialsProvider).withClientConfiguration(clientConfiguration)
                    .withPathStyleAccessEnabled(true).withEndpointConfiguration(endpointConfiguration).build();
        }
    
        public int countObjects(String bucketName) {
            int count = 0;
            ObjectListing objectListing = amazonS3.listObjects(bucketName);
            int currentBatchCount = objectListing.getObjectSummaries().size();
            while (currentBatchCount != 0) {
                count += currentBatchCount;
                objectListing = amazonS3.listNextBatchOfObjects(objectListing);
                currentBatchCount = objectListing.getObjectSummaries().size();
            }
            return count;
        }
    }
    
    0 讨论(0)
  • 2020-12-02 05:10

    As of November 18, 2020 there is now an easier way to get this information without taxing your API requests:

    AWS S3 Storage Lens

    The default, built-in, free dashboard allows you to see the count for all buckets, or individual buckets under the "Buckets" tab. There are many drop downs to filter and sort almost any reasonable metric you would look for.

    0 讨论(0)
  • 2020-12-02 05:11

    There is no way, unless you

    1. list them all in batches of 1000 (which can be slow and suck bandwidth - amazon seems to never compress the XML responses), or

    2. log into your account on S3, and go Account - Usage. It seems the billing dept knows exactly how many objects you have stored!

    Simply downloading the list of all your objects will actually take some time and cost some money if you have 50 million objects stored.

    Also see this thread about StorageObjectCount - which is in the usage data.

    An S3 API to get at least the basics, even if it was hours old, would be great.

    0 讨论(0)
  • 2020-12-02 05:11

    aws s3 ls s3://bucket-name/folder-prefix-if-any --recursive | wc -l

    0 讨论(0)
  • 2020-12-02 05:12

    If you are using AWS CLI on Windows, you can use the Measure-Object from PowerShell to get the total counts of files, just like wc -l on *nix.

    PS C:\> aws s3 ls s3://mybucket/ --recursive | Measure-Object
    
    Count    : 25
    Average  :
    Sum      :
    Maximum  :
    Minimum  :
    Property :
    

    Hope it helps.

    0 讨论(0)
提交回复
热议问题