将对象上传到 Amazon S3 存储桶 - Amazon Tools for PowerShell (第 4 版)
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

Amazon Tools for PowerShell V4 已进入维护模式。

我们建议您迁移到 Amazon Tools for PowerShell V5。有关如何迁移的更多详细信息和信息,请参阅我们的维护模式公告

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

将对象上传到 Amazon S3 存储桶

使用 Write-S3Object cmdlet 将文件作为对象从本地文件系统上传到 Amazon S3 存储桶中。以下示例创建两个简单的 HTML 文件并将其上传到 Amazon S3 存储桶,然后验证上传的对象是否存在。-File 参数和 Write-S3Object 指定本地文件系统中文件的名称。-Key 参数指定 Amazon S3 中对应的对象将具有的名称。

Amazon 从文件扩展名来推断对象的内容类型,在本例中为“.html”。

PS > # Create the two files using here-strings and the Set-Content cmdlet PS > $index_html = @" >> <html> >> <body> >> <p> >> Hello, World! >> </p> >> </body> >> </html> >> "@ >> PS > $index_html | Set-Content index.html PS > $error_html = @" >> <html> >> <body> >> <p> >> This is an error page. >> </p> >> </body> >> </html> >> "@ >> >>$error_html | Set-Content error.html >># Upload the files to Amazon S3 using a foreach loop >>foreach ($f in "index.html", "error.html") { >> Write-S3Object -BucketName website-example -File $f -Key $f -CannedACLName public-read >> } >> PS > # Verify that the files were uploaded PS > Get-S3BucketWebsite -BucketName website-example IndexDocumentSuffix ErrorDocument ------------------- ------------- index.html error.html

标准 ACL 选项

利用 Tools for Windows PowerShell 指定标准 ACL 时所用的值,与 适用于 .NET 的 Amazon SDK 所用的值相同。但要注意,这些值不同于 Amazon S3 Put Object 操作使用的值。Tools for Windows PowerShell 支持以下标准 ACL:

  • NoACL

  • 私有

  • public-read

  • public-read-write

  • aws-exec-read

  • authenticated-read

  • bucket-owner-read

  • bucket-owner-full-control

  • log-delivery-write

有关这些标准 ACL 设置的更多信息,请参阅访问控制列表概述

关于分段上传的备注

如果您使用 Amazon S3 API 上传大于 5 GB 大小的文件,则需要使用分段上传。但是,Tools for Windows PowerShell 提供的 Write-S3Object cmdlet 可以透明地处理大于 5 GB 的上传文件。

测试网站

此时,您可以通过使用浏览器导航到网站来测试网站。托管在 Amazon S3 中的静态网站的 URL 遵循标准格式。

http://<bucket-name>.s3-website-<region>.amazonaws.com

例如:

http://website-example.s3-website-us-west-1.amazonaws.com

另请参阅