除非我遗漏了什么,否则我所看过的所有api似乎都不会告诉您<S3 bucket>/<文件夹>中有多少对象。有办法统计一下吗?
当前回答
如果您在Windows上使用AWS CLI,您可以使用PowerShell中的measurement - object来获得文件的总数,就像*nix上的wc -l一样。
PS C:\> aws s3 ls s3://mybucket/ --recursive | Measure-Object
Count : 25
Average :
Sum :
Maximum :
Minimum :
Property :
希望能有所帮助。
其他回答
在AWS CLI的命令行中,使用ls加——summary。它将为您提供所有项目的列表以及特定bucket中的文档总数。我还没有尝试过包含子桶的桶:
aws s3 ls "s3://MyBucket" --summarize
这可能会花费一些时间(列出我16+K的文档大约需要4分钟),但这比一次计算1K要快。
以下是如何使用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;
}
}
截至2020年11月18日,现在有一种更简单的方法来获取这些信息,而无需对API请求征税:
AWS S3存储镜头
默认的,内置的,免费的仪表板允许您查看所有桶的计数,或在“buckets”选项卡下的单个桶。有许多下拉列表可以过滤和排序几乎任何你想要寻找的合理指标。
您可以使用s3的AWS cloudwatch指标来查看每个桶的确切计数。
如果您在Windows上使用AWS CLI,您可以使用PowerShell中的measurement - object来获得文件的总数,就像*nix上的wc -l一样。
PS C:\> aws s3 ls s3://mybucket/ --recursive | Measure-Object
Count : 25
Average :
Sum :
Maximum :
Minimum :
Property :
希望能有所帮助。
推荐文章
- 如何查看所有地区所有正在运行的Amazon EC2实例?
- 确定记录是否存在的最快方法
- 如何在Python中获得所有直接子目录
- 如何从命令行使用多个AWS帐户?
- 即使模板文件存在,Flask也会引发TemplateNotFound错误
- 如何在Ruby中创建文件
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- __FILE__宏显示完整路径
- 如何搜索亚马逊s3桶?
- 如何将文件指针(file * fp)转换为文件描述符(int fd)?
- 拒绝访问;您需要(至少一个)SUPER特权来执行此操作
- 我如何使用通配符“cp”一组文件与AWS CLI
- 如何删除文件中的特定行?
- 使用Java重命名文件
- 如何从Python包内读取(静态)文件?