除非我遗漏了什么,否则我所看过的所有api似乎都不会告诉您<S3 bucket>/<文件夹>中有多少对象。有办法统计一下吗?
当前回答
Aws s3 ls s3://bucket-name/folder-prefix-if-any——recursive | wc -l .使用实例
其他回答
@Mayank Jaiswal提到的关于使用cloudwatch指标的问题实际上不应该是一个问题。如果你没有得到结果,那可能是因为你的范围不够广。现在是11月3日,无论我做什么尝试,我都没有得到结果。我打开s3桶,查看计数,“对象总数”计数的最后一个记录是11月1日。
下面是cloudwatch解决方案使用javascript aws-sdk的样子:
import aws from 'aws-sdk';
import { startOfMonth } from 'date-fns';
const region = 'us-east-1';
const profile = 'default';
const credentials = new aws.SharedIniFileCredentials({ profile });
aws.config.update({ region, credentials });
export const main = async () => {
const cw = new aws.CloudWatch();
const bucket_name = 'MY_BUCKET_NAME';
const end = new Date();
const start = startOfMonth(end);
const results = await cw
.getMetricStatistics({
// @ts-ignore
Namespace: 'AWS/S3',
MetricName: 'NumberOfObjects',
Period: 3600 * 24,
StartTime: start.toISOString(),
EndTime: end.toISOString(),
Statistics: ['Average'],
Dimensions: [
{ Name: 'BucketName', Value: bucket_name },
{ Name: 'StorageType', Value: 'AllStorageTypes' },
],
Unit: 'Count',
})
.promise();
console.log({ results });
};
main()
.then(() => console.log('Done.'))
.catch((err) => console.error(err));
请注意两点:
范围的开始被设置为月初 周期设置为一天。如果少了一点,您可能会得到一个错误,说您请求了太多的数据点。
3Hub已停产。有一个更好的解决方案,你可以使用传输(仅Mac),然后你只需连接到你的桶,并从视图菜单中选择显示项目计数。
以下是如何使用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;
}
}
在AWS CLI的命令行中,使用ls加——summary。它将为您提供所有项目的列表以及特定bucket中的文档总数。我还没有尝试过包含子桶的桶:
aws s3 ls "s3://MyBucket" --summarize
这可能会花费一些时间(列出我16+K的文档大约需要4分钟),但这比一次计算1K要快。
旧线程,但仍然相关,因为我正在寻找答案,直到我明白了这一点。我想使用基于gui的工具(即没有代码)进行文件计数。我碰巧已经使用了一个名为3Hub的工具,用于在S3之间进行拖放传输。我想知道我在一个特定的存储区中有多少文件(我不认为计费是按存储区划分的)。
So, using 3Hub,
- list the contents of the bucket (looks basically like a finder or explorer window)
- go to the bottom of the list, click 'show all'
- select all (ctrl+a)
- choose copy URLs from right-click menu
- paste the list into a text file (I use TextWrangler for Mac)
- look at the line count
我在桶中有20521个文件,并且在不到一分钟的时间内完成了文件计数。
推荐文章
- 亚马逊ECS和亚马逊EC2有什么区别?
- Python列表目录,子目录和文件
- 我如何知道我在S3桶中存储了多少对象?
- S3 Bucket操作不应用于任何资源
- 将AWS凭证传递给Docker容器的最佳方法是什么?
- 当权限为S3时,AccessDenied for ListObjects for S3 bucket:*
- 电子邮件地址未验证(AWS SES)
- 如果文件不存在,创建文件
- 使用Boto3将S3对象作为字符串打开
- 是否有可能在MATLAB中每个文件定义多个函数,并从该文件外部访问它们?
- 如何使用批处理文件写入文本文件?
- Git复制文件保存历史
- AWS VPC - Internet网关vs. NAT
- 如何将字典保存到文件?
- 如何在AWS Lambda中加载npm模块?