编程语言/S3:修订间差异
来自康健生活
| (未显示同一用户的17个中间版本) | |||
| 第8行: | 第8行: | ||
列出存储桶 | 列出存储桶 | ||
=== aws s3 ls === | |||
{{Shc_out|<syntaxhighlight lang="bash"> | {{Shc_out|<syntaxhighlight lang="bash"> | ||
aws s3 ls</syntaxhighlight>|<syntaxhighlight lang="bash"> | aws s3 ls</syntaxhighlight>|<syntaxhighlight lang="bash"> | ||
| 第19行: | 第20行: | ||
2021-07-27 11:07:03 medici-static | 2021-07-27 11:07:03 medici-static | ||
2021-10-15 11:26:29 medici-yangyalin1 | 2021-10-15 11:26:29 medici-yangyalin1 | ||
</syntaxhighlight>|$|/home/kangkk}} | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
aws s3 ls --summarize --human-readable --recursive s3://medici-plugin/</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
2022-03-22 16:18:56 16.6 KiB _.fonts.qihoo.net.zip | |||
... | |||
2022-03-17 17:57:17 2.4 MiB sketch/2.0.2/net.qihoo.design.beta.2.0.2.zip | |||
2022-03-17 19:23:52 2.4 MiB sketch/2.0.2/net.qihoo.design.production.2.0.2.zip | |||
Total Objects: 36 | |||
Total Size: 43.8 MiB | |||
</syntaxhighlight>|$|/home/kangkk}} | </syntaxhighlight>|$|/home/kangkk}} | ||
| 第25行: | 第37行: | ||
aws s3 ls s3://medici-static --recursive | grep -v -E "(Bucket: |Prefix: |LastWriteTime|^$|--)" | awk 'BEGIN {total=0}{total+=$3}END{print total/1024/1024" MB"}'</syntaxhighlight>|<syntaxhighlight lang="bash"> | aws s3 ls s3://medici-static --recursive | grep -v -E "(Bucket: |Prefix: |LastWriteTime|^$|--)" | awk 'BEGIN {total=0}{total+=$3}END{print total/1024/1024" MB"}'</syntaxhighlight>|<syntaxhighlight lang="bash"> | ||
3.5634 MB | 3.5634 MB | ||
</syntaxhighlight>|$|/home/kangkk}} | |||
=== aws s3api === | |||
或者使用 s3api 对于大桶也很慢。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
aws s3api list-objects --bucket medici-plugin --output json --query "[sum(Contents[].Size), length(Contents[])]"</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
[ | |||
45909139, | |||
36 | |||
] | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
aws s3api list-objects --bucket medici-static --output json --query "[sum(Contents[].Size), length(Contents[])]" | awk 'NR!=2 {print $0;next} NR==2 {print $0/1024/1024/1024" GB"}'</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
[ | |||
0.00347988 GB | |||
51 | |||
] | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
==== 日期 ==== | |||
定义日期<ref>列表对象-v2. https://docs.aws.amazon.com/cli/latest/reference/s3api/list-objects-v2.html</ref>。定义 <code>DATE</code> 日期格式为 <code>%Y-%m-%d</code>,也就是 <code>YYYY-MM-DD</code>。 | |||
给定日期的所有文件。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
# aws s3api list-objects-v2 --bucket <S3_BUCKET_NAME> --query 'Contents[? contains(LastModified, `$DATE`)]' | |||
aws s3api list-objects-v2 --bucket abc-plugin --query 'Contents[?contains(LastModified, `2021-08-12`)].Key' | |||
</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
[ | |||
"net.abc.xyz.0.6.0.exe", | |||
"net.abc.xyz.0.6.0.pkg", | |||
"net.abc.xyz.0.6.1.exe", | |||
"net.abc.xyz.0.6.1.pkg", | |||
"net.abc.xyz.0.7.1.zip", | |||
"net.abc.xyz.0.8.1.zip", | |||
"net.abc.xyz.0.8.2.zip" | |||
] | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
某个日期之后的所有文件。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
aws s3api list-objects-v2 --bucket <S3_BUCKET_NAME> --query 'Contents[?LastModified >= `$DATE`]' | |||
</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
aws s3api list-objects-v2 --bucket <S3_BUCKET_NAME> --query 'Contents[?LastModified >= `$DATE` && LastModified<=`$DATE`]' | |||
</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
统计数量 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
aws s3api list-objects-v2 --bucket abc-plugin --output json --query "[sum(Contents[?LastModified>='2021-08-12'].Size), length(Contents[?LastModified>='2021-08-12'])]" | awk 'NR!=2 {print $0;next} NR==2 {print $0/1024/1024/1024" GB"}'</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
[ | |||
45909139, | |||
36 | |||
] | |||
</syntaxhighlight>|$|/home/kangkk}} | </syntaxhighlight>|$|/home/kangkk}} | ||
| 第61行: | 第130行: | ||
aws_access_key_id=abcdefg | aws_access_key_id=abcdefg | ||
aws_secret_access_key=1234567890</pre>}} | aws_secret_access_key=1234567890</pre>}} | ||
=== profiles === | |||
指定 profiles。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
aws s3 sync s3://abc-plugin s3://abc-development/plugin --profile pub-xyz-s3</syntaxhighlight>|$|/home/kangkk}} | |||
=== 命令 === | === 命令 === | ||
| 第86行: | 第160行: | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
aws s3 sync s3://bucket_source_name s3://bucket_target_name</syntaxhighlight>|$|/home/kangkk}} | aws s3 sync s3://bucket_source_name s3://bucket_target_name</syntaxhighlight>|$|/home/kangkk}} | ||
==== 管道 ==== | |||
跨区域转移文件。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
aws s3 cp s3://abc-plugin/file - --profile pub-xyz-s3 | aws s3 cp - s3://abc-prod-static/plugin/file --profile beijing-xyz-s3</syntaxhighlight>|$|/home/kangkk}} | |||
==== 迁移 ==== | ==== 迁移 ==== | ||
| 第339行: | 第418行: | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
== 调试 == | === 调试 === | ||
{{Shc_out|<syntaxhighlight lang="bash"> | {{Shc_out|<syntaxhighlight lang="bash"> | ||
s3cmd du s3://medici-kangkk -d | s3cmd du s3://medici-kangkk -d | ||
| 第360行: | 第439行: | ||
... | ... | ||
ERROR: S3 error: 403 (InvalidAccessKeyId): The AWS Access Key Id you provided does not exist in our records. | ERROR: S3 error: 403 (InvalidAccessKeyId): The AWS Access Key Id you provided does not exist in our records. | ||
</syntaxhighlight>|$|/home/kangkk}} | |||
=== 查看 === | |||
查看桶的已使用大小 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
s3cmd du s3://medici-static</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
3736491 51 objects s3://medici-static/ | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
添加参数更改单位 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
s3cmd du -H s3://medici-static</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
3M 51 objects s3://medici-static/ | |||
</syntaxhighlight>|$|/home/kangkk}} | </syntaxhighlight>|$|/home/kangkk}} | ||
== 上传 == | == 上传 == | ||
== 转存 == | == 转存 == | ||
== node == | |||
=== 示例 === | |||
<syntaxhighlight lang="javascript"> | |||
// use think.js | |||
const {bucket, options} = this.config('s3'); | |||
const s3 = new aws.S3(new aws.Config({ | |||
...options, | |||
httpOptions: {timeout: 3000} | |||
})); | |||
let result = null; | |||
try { | |||
result = await s3.getObject({ | |||
Bucket: this.bucket, | |||
Key: s3Key | |||
}).promise(); | |||
} catch (error) {}</syntaxhighlight> | |||
读取的时候如果文件体积较大,可以考虑直接使用流进行处理。 | |||
<syntaxhighlight lang="javascript"> | |||
module.exports = class extends BaseRest { | |||
async getAction() { | |||
const pipe = s3.getObject({Bucket: bucket, Key: name}).createReadStream(); | |||
pipe.on('error', err => { | |||
err.status = err.statusCode; | |||
throw err; | |||
}); | |||
this.ctx.type = this.id.match(/\.(\w+)$/)[1]; | |||
this.ctx.body = pipe; | |||
} | |||
};</syntaxhighlight> | |||
== error == | == error == | ||
* bash: aws: command not found | * bash: aws: command not found | ||
| 第397行: | 第521行: | ||
/home/kangjian/.local/bin:/home/kangjian/bin:/root/perl5/bin:/sbin:/bin:/usr/sbin:/usr/bin | /home/kangjian/.local/bin:/home/kangjian/bin:/root/perl5/bin:/sbin:/bin:/usr/sbin:/usr/bin | ||
</syntaxhighlight>|$|/home/kangkk}} | </syntaxhighlight>|$|/home/kangkk}} | ||
== 参考 == | |||
* [https://docs.aws.amazon.com/cli/latest/reference/s3/sync.html AWS CLI Command Reference] | |||
* [https://docs.aws.amazon.com/cli/latest/topic/config-vars.html#cli-aws-help-config-vars AWS CLI Configuration Variables] | |||
* [https://docs.amazonaws.cn/cli/latest/userguide/cli-configure-files.html 配置和凭证文件设置] | |||
* [https://docs.amazonaws.cn/cli/latest/userguide/cli-configure-profiles.html Amazon CLI 的命名配置文件] | |||