查看“︁编程语言/S3”︁的源代码
来自康健生活
←
编程语言/S3
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 引言 == == 查看 == 查看版本信息。 {{Shc_out|<syntaxhighlight lang="bash"> aws --version</syntaxhighlight>|<syntaxhighlight lang="bash"> aws-cli/1.22.26 Python/3.9.12 Darwin/19.6.0 botocore/1.23.26 </syntaxhighlight>|$|/home/kangkk}} 列出存储桶 === aws s3 ls === {{Shc_out|<syntaxhighlight lang="bash"> aws s3 ls</syntaxhighlight>|<syntaxhighlight lang="bash"> 2021-07-26 21:27:26 medici-audit 2021-03-27 21:11:24 medici-beta 2020-08-16 05:05:48 medici-dev 2021-04-09 16:34:30 medici-development 2021-10-15 17:18:35 medici-kangkk 2021-08-12 09:15:54 medici-plugin 2020-08-16 05:05:55 medici-prod 2021-07-27 11:07:03 medici-static 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}} 查看 S3 的使用情况。如果桶很大会计算很慢。 {{Shc_out|<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 </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>。 给定日期的所有文件。 {{Shc_out|<syntaxhighlight lang="bash"> # DATE=$(date +%Y-%m-%d) aws s3api list-objects-v2 --bucket test-bucket-fh --query 'Contents[? contains(LastModified, `$DATE`)]'</syntaxhighlight>|<syntaxhighlight lang="bash"> </syntaxhighlight>|$|/home/kangkk}} 某个日期之后的所有文件。 {{Shc_out|<syntaxhighlight lang="bash"> # YESTERDAY=`date -v-1w +%F` aws s3api list-objects-v2 --bucket test-bucket-fh --query 'Contents[? LastModified > `$YESTERDAY`)]' </syntaxhighlight>|<syntaxhighlight lang="bash"> </syntaxhighlight>|$|/home/kangkk}} == aws == {{Shc_out|<syntaxhighlight lang="bash"> pip3 install --upgrade pip && pip3 install awscli awscli-plugin-endpoint && aws --version</syntaxhighlight>|<syntaxhighlight lang="bash">aws-cli/1.22.26 Python/3.9.9 Darwin/19.6.0 botocore/1.23.26 </syntaxhighlight>|$|/home/kangkk}} === 配置文件 === 执行命令进行配置文件的创建。 {{shc|<syntaxhighlight lang="bash"> aws configure</syntaxhighlight>|$|/home/kangkk}} 或者也可以手动创建配置文件到目标文件夹 <code>~/.aws</code>。 {{shc|<syntaxhighlight lang="bash"> mkdir ~/.aws</syntaxhighlight>|$|/home/kangkk}} 然后编写配置文件。 {{Hc|config|<pre> [plugins] endpoint = awscli_plugin_endpoint [default] region = us-east-1 output = json s3 = endpoint_url = http://xxx.xyz signature_version = s3v4 s3api = endpoint_url = http://xxx.xyz</pre>}} 和 {{Hc|credentials|<pre> [default] aws_access_key_id=abcdefg aws_secret_access_key=1234567890 [pub-bjyt-s3] aws_access_key_id=abcdefg 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}} === 命令 === ==== 删除 ==== 删除文件夹 {{shc|<syntaxhighlight lang="bash"> aws s3 rm s3://bucket_name/folder --recursive</syntaxhighlight>|$|/home/kangkk}} <div class="pageBoxGreen">recursive: 递归</div> ==== 上传 ==== 上传文件。 {{shc|<syntaxhighlight lang="bash"> aws s3 cp /home/kangkk/folder_name/file.zip s3://bucket_name/folder/</syntaxhighlight>|$|/home/kangkk}} 上传文件夹 {{shc|<syntaxhighlight lang="bash"> aws s3 cp /home/kangkk/folder_name/ s3://bucket_name/folder/ --recursive</syntaxhighlight>|$|/home/kangkk}} ==== 下载 ==== {{shc|<syntaxhighlight lang="bash"> aws s3 cp s3://bucket_name/folder/ /home/kangkk/folder_name --recursive </syntaxhighlight>|$|/home/kangkk}} ==== 备份 ==== 迁移/备份 bucket 的内容到另一个 bucket。 {{shc|<syntaxhighlight lang="bash"> 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}} ==== 迁移 ==== {{shc|<syntaxhighlight lang="bash"> aws s3 mv s3://bucket_source_name s3://bucket_target_name</syntaxhighlight>|$|/home/kangkk}} === help === {{Shc_out|<syntaxhighlight lang="bash"> aws s3 help</syntaxhighlight>|<syntaxhighlight lang="text"> S3() S3() NAME s3 - DESCRIPTION This section explains prominent concepts and notations in the set of high-level S3 commands provided. Path Argument Type Whenever using a command, at least one path argument must be specified. There are two types of path arguments: LocalPath and S3Uri. LocalPath: represents the path of a local file or directory. It can be written as an absolute path or relative path. S3Uri: represents the location of a S3 object, prefix, or bucket. This must be written in the form s3://mybucket/mykey where mybucket is the specified S3 bucket, mykey is the specified S3 key. The path argument must begin with s3:// in order to denote that the path argument refers to a S3 object. Note that prefixes are separated by forward slashes. For example, if the S3 object myobject had the prefix myprefix, the S3 key would be myprefix/myobject, and if the object was in the bucket mybucket, the S3Uri would be s3://mybucket/myprefix/myobject. S3Uri also supports S3 access points. To specify an access point, this value must be of the form s3://<access-point-arn>/<key>. For example if the access point myaccesspoint to be used has the ARN: arn:aws:s3:us-west-2:123456789012:accesspoint/myaccesspoint and the object being accessed has the key mykey, then the S3URI used must be: s3://arn:aws:s3:us-west-2:123456789012:accesspoint/myaccesspoint/mykey. Similar to bucket names, you can also use prefixes with access point ARNs for the S3Uri. For example: s3://arn:aws:s3:us-west-2:123456789012:accesspoint/myaccesspoint/mypre- fix/ The higher level s3 commands do not support access point object ARNs. For example, if the following was specified: s3://arn:aws:s3:us-west-2:123456789012:accesspoint/myaccess- point/object/mykey the S3URI will resolve to the object key object/mykey Order of Path Arguments Every command takes one or two positional path arguments. The first path argument represents the source, which is the local file/directory or S3 object/prefix/bucket that is being referenced. If there is a second path argument, it represents the destination, which is the local file/directory or S3 object/prefix/bucket that is being operated on. Commands with only one path argument do not have a destination because the operation is being performed only on the source. Single Local File and S3 Object Operations Some commands perform operations only on single files and S3 objects. The following commands are single file/object operations if no --recur- sive flag is provided. o cp o mv o rm For this type of operation, the first path argument, the source, must exist and be a local file or S3 object. The second path argument, the destination, can be the name of a local file, local directory, S3 object, S3 prefix, or S3 bucket. The destination is indicated as a local directory, S3 prefix, or S3 bucket if it ends with a forward slash or back slash. The use of slash depends on the path argument type. If the path argument is a Local- Path, the type of slash is the separator used by the operating system. If the path is a S3Uri, the forward slash must always be used. If a slash is at the end of the destination, the destination file or object will adopt the name of the source file or object. Otherwise, if there is no slash at the end, the file or object will be saved under the name provided. See examples in cp and mv to illustrate this description. Directory and S3 Prefix Operations Some commands only perform operations on the contents of a local direc- tory or S3 prefix/bucket. Adding or omitting a forward slash or back slash to the end of any path argument, depending on its type, does not affect the results of the operation. The following commands will always result in a directory or S3 prefix/bucket operation: o sync o mb o rb o ls Use of Exclude and Include Filters Currently, there is no support for the use of UNIX style wildcards in a command's path arguments. However, most commands have --exclude "<value>" and --include "<value>" parameters that can achieve the desired result. These parameters perform pattern matching to either exclude or include a particular file or object. The following pattern symbols are supported. o *: Matches everything o ?: Matches any single character o [sequence]: Matches any character in sequence o [!sequence]: Matches any character not in sequence Any number of these parameters can be passed to a command. You can do this by providing an --exclude or --include argument multiple times, e.g. --include "*.txt" --include "*.png". When there are multiple filters, the rule is the filters that appear later in the command take precedence over filters that appear earlier in the command. For exam- ple, if the filter parameters passed to the command were --exclude "*" --include "*.txt" All files will be excluded from the command except for files ending with .txt However, if the order of the filter parameters was changed to --include "*.txt" --exclude "*" All files will be excluded from the command. Each filter is evaluated against the source directory. If the source location is a file instead of a directory, the directory containing the file is used as the source directory. For example, suppose you had the following directory structure: /tmp/foo/ .git/ |---config |---description foo.txt bar.txt baz.jpg In the command aws s3 sync /tmp/foo s3://bucket/ the source directory is /tmp/foo. Any include/exclude filters will be evaluated with the source directory prepended. Below are several examples to demonstrate this. Given the directory structure above and the command aws s3 cp /tmp/foo s3://bucket/ --recursive --exclude ".git/*", the files .git/config and .git/description will be excluded from the files to upload because the exclude filter .git/* will have the source prepended to the filter. This means that: /tmp/foo/.git/* -> /tmp/foo/.git/config (matches, should exclude) /tmp/foo/.git/* -> /tmp/foo/.git/description (matches, should exclude) /tmp/foo/.git/* -> /tmp/foo/foo.txt (does not match, should include) /tmp/foo/.git/* -> /tmp/foo/bar.txt (does not match, should include) /tmp/foo/.git/* -> /tmp/foo/baz.jpg (does not match, should include) The command aws s3 cp /tmp/foo/ s3://bucket/ --recursive --exclude "ba*" will exclude /tmp/foo/bar.txt and /tmp/foo/baz.jpg: /tmp/foo/ba* -> /tmp/foo/.git/config (does not match, should include) /tmp/foo/ba* -> /tmp/foo/.git/description (does not match, should include) /tmp/foo/ba* -> /tmp/foo/foo.txt (does not match, should include) /tmp/foo/ba* -> /tmp/foo/bar.txt (matches, should exclude) /tmp/foo/ba* -> /tmp/foo/baz.jpg (matches, should exclude) Note that, by default, all files are included. This means that provid- ing only an --include filter will not change what files are trans- ferred. --include will only re-include files that have been excluded from an --exclude filter. If you only want to upload files with a par- ticular extension, you need to first exclude all files, then re-include the files with the particular extension. This command will upload only files ending with .jpg: aws s3 cp /tmp/foo/ s3://bucket/ --recursive --exclude "*" --include "*.jpg" If you wanted to include both .jpg files as well as .txt files you can run: aws s3 cp /tmp/foo/ s3://bucket/ --recursive \ --exclude "*" --include "*.jpg" --include "*.txt" See 'aws help' for descriptions of global parameters. SYNOPSIS aws s3 <Command> [<Arg> ...] OPTIONS None See 'aws help' for descriptions of global parameters. AVAILABLE COMMANDS o cp o ls o mb o mv o presign o rb o rm o sync o website S3() (END) </syntaxhighlight>|$|/home/kangkk}} == s3cmd == === 安装 === {{shc|<syntaxhighlight lang="bash"> pip3 install s3cmd</syntaxhighlight>|$|/home/kangkk}} === 配置文件 === {{Hc|.s3cfg|<syntaxhighlight lang="javascript"> [default] access_key = <access_key> secret_key = <secret_key> bucket_location = <region> host_base = <endpoint> host_bucket = <endpoint> use_https = False human_readable_sizes = True website_index = index.html signature_v2 = True </syntaxhighlight>}} 可简略为 {{Hc|.s3cfg|<syntaxhighlight lang="javascript"> [default] access_key = <access_key> secret_key = <secret_key> host_base = <endpoint> host_bucket = <endpoint> use_https = False </syntaxhighlight>}} === 调试 === {{Shc_out|<syntaxhighlight lang="bash"> s3cmd du s3://medici-kangkk -d </syntaxhighlight>|<syntaxhighlight lang="bash"> DEBUG: s3cmd version 2.2.0 DEBUG: ConfigParser: Reading file '/Users/kangjian/.s3cfg' DEBUG: ConfigParser: access_key->Nl...17_chars...U DEBUG: ConfigParser: secret_key->bL...37_chars...B DEBUG: ConfigParser: host_base->http://pub1-bjyt.s3.360.cn DEBUG: Updating Config.Config cache_file -> DEBUG: Updating Config.Config follow_symlinks -> False DEBUG: Updating Config.Config verbosity -> 10 DEBUG: Command: du DEBUG: CreateRequest: resource[uri]=/ DEBUG: ===== SEND Inner request to determine the bucket region ===== DEBUG: CreateRequest: resource[uri]=/ DEBUG: Using signature v4 DEBUG: get_hostname(medici-kangkk): pub1-bjyt.s3.360.cn DEBUG: canonical_headers = host:pub1-bjyt.s3.360.cn ... 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}} == 上传 == == 转存 == == 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 == * bash: aws: command not found 同时安装 python2 和 python3,导致冲突。修复冲突。 {{Shc_out|<syntaxhighlight lang="bash"> pip3 install awscli --ignore-installed six</syntaxhighlight>|<syntaxhighlight lang="bash"> </syntaxhighlight>|$|/home/kangkk}} 如果失败,则手动配置路径(mac), {{Hc|~/.local/.bashprofile|<syntaxhighlight lang="javascript"> export PATH=~/.local/bin:$PATH</syntaxhighlight>}} linux. {{shc|<syntaxhighlight lang="bash"> echo $PATH | grep ~/bin</syntaxhighlight>|$|/home/kangkk}} 如果内容是空的,则。 {{shc|<syntaxhighlight lang="bash"> export PATH=~/bin:$PATH</syntaxhighlight>|$|/home/kangkk}} {{Shc_out|<syntaxhighlight lang="bash"> echo $PATH | grep ~/bin</syntaxhighlight>|<syntaxhighlight lang="bash"> /home/kangjian/bin:/root/perl5/bin:/sbin:/bin:/usr/sbin:/usr/bin </syntaxhighlight>|$|/home/kangkk}} {{Shc_out|<syntaxhighlight lang="bash"> ls -a ~</syntaxhighlight>|<syntaxhighlight lang="bash"> ~ . .. .aws .bash_history .bash_logout .bash_profile .bashrc .cache .local .viminfo </syntaxhighlight>|$|/home/kangkk}} {{shc|<syntaxhighlight lang="bash"> export PATH=~/.local/bin:$PATH</syntaxhighlight>|$|/home/kangkk}} {{Shc_out|<syntaxhighlight lang="bash"> echo $PATH | grep ~/bin</syntaxhighlight>|<syntaxhighlight lang="bash"> /home/kangjian/.local/bin:/home/kangjian/bin:/root/perl5/bin:/sbin:/bin:/usr/sbin:/usr/bin </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 的命名配置文件]
此页面嵌入的页面:
模板:Hc
(
查看源代码
)
模板:Hc/style.css
(
查看源代码
)
模板:Shc
(
查看源代码
)
模板:Shc/style.css
(
查看源代码
)
模板:Shc out
(
查看源代码
)
模板:Shc out/style.css
(
查看源代码
)
返回
编程语言/S3
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。