除非我遗漏了什么,否则我所看过的所有api似乎都不会告诉您<S3 bucket>/<文件夹>中有多少对象。有办法统计一下吗?
当前回答
转到AWS计费,然后是报表,然后是AWS使用情况报表。 选择Amazon Simple Storage Service,然后选择Operation StandardStorage。 然后,您可以下载一个CSV文件,该文件包含StorageObjectCount的UsageType,该文件列出每个桶的项目计数。
其他回答
您可以使用s3的AWS cloudwatch指标来查看每个桶的确切计数。
没有一个API会给你一个计数,因为真的没有任何亚马逊特定的API来做这件事。您只需运行一个list-contents并计算返回的结果的数量。
以下是如何使用java客户端。
<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;
}
}
api将以1000为增量返回列表。检查IsTruncated属性,看看是否还有更多。如果有,您需要进行另一次调用,并在下次调用时传递您获得的最后一个键作为Marker属性。然后继续这样循环,直到IsTruncated为false。
有关更多信息,请参阅亚马逊文档:遍历多页结果
如果你正在寻找特定的文件,比如.jpg图像,你可以执行以下操作:
aws s3 ls s3://your_bucket | grep jpg | wc -l
推荐文章
- 如何将文件指针(file * fp)转换为文件描述符(int fd)?
- 拒绝访问;您需要(至少一个)SUPER特权来执行此操作
- 我如何使用通配符“cp”一组文件与AWS CLI
- 如何删除文件中的特定行?
- 使用Java重命名文件
- 如何从Python包内读取(静态)文件?
- 为什么我得到“Pickle - EOFError: run out of input”读取一个空文件?
- 我如何获得亚马逊的AWS_ACCESS_KEY_ID ?
- 如何使所有对象在AWS S3桶公共默认?
- 在PostgreSQL中快速发现表的行数
- 写字符串到文本文件,并确保它总是覆盖现有的内容。
- 从另一个文件导入变量?
- 为什么我应该使用亚马逊Kinesis而不是SNS-SQS?
- 寻找不属于特定用户的文件
- 如何复制文件跨计算机使用SSH和MAC OS X终端