Programming Amazon Web Services: S3, EC2, SQS, FPS, and SimpleDB (Paperback)
暫譯: 編程亞馬遜網路服務:S3、EC2、SQS、FPS 與 SimpleDB (平裝本)
James Murty
- 出版商: O'Reilly
- 出版日期: 2008-04-29
- 售價: $1,880
- 貴賓價: 9.5 折 $1,786
- 語言: 英文
- 頁數: 604
- 裝訂: Paperback
- ISBN: 0596515812
- ISBN-13: 9780596515812
-
相關分類:
Amazon Web Services
海外代購書籍(需單獨結帳)
買這商品的人也買了...
-
$880$695 -
$1,550$1,473 -
$360$284 -
$860$731 -
$780$616 -
$680$530 -
$299$236 -
$1,860$1,767 -
$490$294 -
$490$387 -
$1,650$1,568 -
$490$387 -
$880$862 -
$520$411 -
$680$537 -
$780$616 -
$1,420$1,349 -
$580$458 -
$500$395 -
$1,570$1,492 -
$1,330Beginning Python: Using Python 2.6 and Python 3.1 (Paperback)
-
$680$537 -
$1,309REST in Practice: Hypermedia and Systems Architecture (Paperback)
-
$1,570$1,492 -
$1,056Programming Elastic MapReduce: Using AWS Services to Build an End-to-End Application (Paperback)
相關主題
商品描述
Building on the success of its storefront and fulfillment services, Amazon now allows businesses to "rent" computing power, data storage and bandwidth on its vast network platform. This book demonstrates how developers working with small- to mid-sized companies can take advantage of Amazon Web Services (AWS) such as the Simple Storage Service (S3), Elastic Compute Cloud (EC2), Simple Queue Service (SQS), Flexible Payments Service (FPS), and SimpleDB to build web-scale business applications. With AWS, Amazon offers a new paradigm for IT infrastructure: use what you need, as you need it, and pay as you go. Programming Web Services explains how you can access Amazon's open APIs to store and run applications, rather than spend precious time and resources building your own. With this book, you'll learn all the technical details you need to: Store and retrieve any amount of data using application servers, unlimited data storage, and bandwidth with the Amazon S3 service Buy computing time using Amazon EC2's interface to requisition machines, load them with an application environment, manage access permissions, and run your image using as many or few systems as needed Use Amazon's web-scale messaging infrastructure to store messages as they travel between computers with Amazon SQS Leverage the Amazon FPS service to structure payment instructions and allow the movement of money between any two entities, humans or computers Create and store multiple data sets, query your data easily, and return the results using Amazon SimpleDB. Scale up or down at a moment's notice, using these services to employ as much time and space as you need Whether you're starting a new online business, need to ramp upexisting services, or require an offsite backup for your home, Programming Web Services gives you the background and the practical knowledge you need to start using AWS. Other books explain how to build web services. This book teaches businesses how to take make use of existing services from an established technology leader.
Create HTML POST Forms That Allow Your Web Site Visitors to Upload Files Into Your S3 Account Using a Standard Web Browser
By James Murty, creator of the JetS3t Java S3 library and author of Programming Amazon Web Services
Amazon’s Simple Storage Service (S3) provides cheap and unlimited online data storage for anyone with a credit card and an Amazon Web Service (AWS) account. If you have an AWS account, you can interact with the S3 service using specialized tools to upload and manage your files. It is very convenient to have access to this online storage resource for yourself, but there may be situations where you would like to allow others to upload files into your account.
For this purpose, S3 accepts uploads via specially-crafted and pre-authorized HTML POST forms. You can include these forms in any web page to allow your web site visitors to send you files using nothing more than a standard web browser.
In this article, I will demonstrate how to build simple S3 POST forms. I will assume that you have already signed up for the S3 service, and that you have an S3 client program for creating buckets and viewing files in your account. Before you proceed, create your own bucket to store uploaded files — in the examples below I will use a bucket named s3-bucket.
POST Form Web Page
Here is a web page with an S3 POST Form that you can use as a template for your own forms:
S3 POST Form
This template demonstrates some important features of an S3 POST form, and the web page that contains it:
- The web page that contains the form has a
meta
tag in the head section that tells web browsers to use theUTF-8
unicode character encoding. - The form’s
action
parameter specifies an S3 URL that includes the name of your destination bucket, in this case the bucket called s3-bucket. - The form contains a number of input fields with parameter names and values that will be sent to the S3 service. If any required input fields are missing, or if a field has an incorrect value, the service will not accept uploads from the form.
The S3 service uses information from the form’s input fields to authorize uploads, and to set the properties of uploaded file objects. Here is a description of the most common input fields:
Field Name | Description |
---|---|
key | A name for the S3 object that will store the uploaded file’s data. This name can be set in advance when you know what information the user will upload, for example: uploads/monthly_report.txt. If you do not know the name of the file a user will upload, the key value can include the special variable |
AWSAccessKeyId | The Access Key Identifier credential for your Amazon Web Service account. |
acl | The access control policy to apply to the uploaded file. If you do not want the uploaded file to be made available to the general public, you should use the value private . To make the uploaded file publicly available, use the value public-read . |
success_action_redirect | The URL address to which the user’s web browser will be redirected after the file is uploaded. This URL should point to a “Successful Upload” page on your web site, so you can inform your users that their files have been accepted. S3 will add bucket , key and etag parameters to this URL value to inform your web application of the location and hash value of the uploaded file. |
policy | A Base64-encoded policy document that applies rules to file uploads sent by the S3 POST form. This document is used to authorize the form, and to impose conditions on the files that can be uploaded. Policy documents will be described in more detail below. |
signature | A signature value that authorizes the form and proves that only you could have created it. This value is calculated by signing the Base64-encoded policy document with your AWS Secret Key, a process that I will demonstrate below. |
Content-Type | The content type (mime type) that will be applied to the uploaded file, for example image/jpeg for JPEG picture files. If you do not know what type of file a user will upload, you can either prompt the user to provide the appropriate content type, or write browser scripting code that will automatically set this value based on the file’s name.If you do not set the content type with this field, S3 will use the default value |
file | The input field that allows a user to select a file to upload. This field must be the last one in the form, as any fields below it are ignored by S3. |
This overview of the form’s input fields should help you to modify the template POST form to suit your own purposes. At a minimum, you will need to edit the form’s action
parameter to point to your own S3 bucket, and set the value of the AWSAccessKeyId
field to your AWS Access Key credential.
To complete the form and make it acceptable to the S3 service, you will also need to generate a policy document and signature value.
Policy Document
S3 POST forms include a policy document that authorizes the form and imposes limits on the files that can be uploaded. When S3 receives a file via a POST form, it will check the policy document and signature to confirm that the form was created by someone who is allowed to store files in the target S3 account.
A policy document is a collection of properties expressed in JavaScript Object Notation, which simply means that the document’s structure and content must conform to a certain format. Every policy document contains two top-level items:
expiration
- A Greenwich Mean Time (GMT) timestamp that specifies when the policy document will expire. Once a policy document has expired, the upload form will no longer work.conditions
- A set of rules to define the values that may be included in the form’s input fields, and to impose size limits for file uploads.
Here is a policy document corresponding to the POST form template above. This policy has an expiration
date of January 1st 2009:
{"expiration": "2009-01-01T00:00:00Z", "conditions": [ {"bucket": "s3-bucket"}, ["starts-with", "$key", "uploads/"], {"acl": "private"}, {"success_action_redirect": "http://localhost/"}, ["starts-with", "$Content-Type", ""], ["content-length-range", 0, 1048576] ] }
To create a valid S3 POST form, you must include a policy document whose conditions
section contains a rule for almost every input field in the form. At a minimum, this document must include rules for the bucket
and key
values of the uploaded file object. In addition to these two rules, you will need to include a rule for every other input field in the form except for AWSAccessKeyId
, signature
, policy
and file
.
Because our template POST form includes the input fields acl
, success_action_redirect
, and Content-Type
, our policy document includes rules corresponding to these fields. Our policy document also includes an extra content-length-range
rule that limits the size of files that can be uploaded.
There are three kinds of rules you can apply in your policy document:
- Equality rule, which checks that an input field’s value is set to a given string. An equality rule is expressed as a name and value pair within brace characters, for example:
{"acl": "private"}
- Starts-With rule, which checks that an input field’s value begins with a given string. If the given string is empty, S3 will check only that the field is present in the form and will not care what value it contains. A starts-with rule is expressed as a three-element array that contains the term
starts-with
, followed by the name of the input field preceded by a$
symbol, then the prefix string value for comparison.
In the policy document above, we use starts-with rules for thekey
andContent-Type
fields because we do not know in advance the name of the file a user will upload, or what type of file it will be. The rule for theContent-Type
field uses an empty string for comparison, which means it will permit any content type value. The rule for the object’skey
name uses the prefix string “upload/”, which means that the key value must always start with the upload/ subdirectory path. - Content length rule, which checks that the size of an uploaded file is between a given minimum and maximum value. If this rule is not included in a policy document, users will be able to upload files of any size up to the 5GB limit imposed by S3.
A content length rule is expressed as a three-element array that contains the termcontent-length-range
, followed by integer values to set the minimum and maximum file size. The policy document above includes a content length rule that will prevent the form from uploading files larger than 1MB in size (1,048,576 bytes).
It is important to make sure that your policy document corresponds exactly to your S3 POST form. If there are any discrepancies between the input field values in your form and the rule values in your policy document, or if your form contains input fields that do not have corresponding rules in your policy, the S3 service will reject the form and return an incomprehensible XML error message to your users.
Sign Your S3 POST Form
To complete your S3 POST form, you must sign it to prove to S3 that you actually created the form. If you do not sign the form properly, or if someone else tries to modify your form after it has been signed, the service will be unable to authorize it and will reject the upload.
To sign your form you need to perform two steps:
- Base64-encode the policy document, and include it in the form’s
policy
input field. - Calculate a signature value (SHA-1 HMAC) from the encoded policy document using your AWS Secret Key credential as a password. Include this value in the form’s
signature
input field after Base64-encoding it.
Almost all programming languages include libraries for performing these two steps. Here are some example code fragments to do the job with different languages, assuming you have already defined the variables policy_document
and aws_secret_key
.
Ruby
require 'base64' require 'openssl' require 'digest/sha1' policy = Base64.encode64(policy_document).gsub("\n","") signature = Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new('sha1'), aws_secret_key, policy) ).gsub("\n","")
Java
import sun.misc.BASE64Encoder; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; String policy = (new BASE64Encoder()).encode( policy_document.getBytes("UTF-8")).replaceAll("\n",""); Mac hmac = Mac.getInstance("HmacSHA1"); hmac.init(new SecretKeySpec( aws_secret_key.getBytes("UTF-8"), "HmacSHA1")); String signature = (new BASE64Encoder()).encode( hmac.doFinal(policy.getBytes("UTF-8"))) .replaceAll("\n", "");
Python
import base64 import hmac, sha policy = base64.b64encode(policy_document) signature = base64.b64encode( hmac.new(aws_secret_key, policy, sha).digest())
Once you have calculated the values for the policy
and signature
input fields and included these values in your form, the form should be complete. Save the web page and form document as an .html
file, open it in your favorite web browser, and test it by uploading some files to your S3 bucket.
Conclusion
The form web page and policy document templates in this article should give you a starting point for creating your own upload forms. With some minor modifications to the template documents and a little coding, you will be able to create authorized forms that make it easy for your web site visitors to upload files to your S3 account.
The S3 service’s POST support is a powerful feature with many potential uses. You could create a single upload form to allow your friends and colleagues to send you files that are too large for email, or you could modify your web applications to generate forms on-demand so your users can store their data in S3 rather than on your own server. Just remember that you will be liable for any S3 data transfer and storage fees incurred by the people who use your forms.
商品描述(中文翻譯)
**產品描述**
基於其商店和履行服務的成功,亞馬遜現在允許企業「租用」其龐大網絡平台上的計算能力、數據存儲和帶寬。本書展示了如何讓在小型到中型企業工作的開發人員利用亞馬遜網絡服務(Amazon Web Services, AWS),例如簡單存儲服務(Simple Storage Service, S3)、彈性計算雲(Elastic Compute Cloud, EC2)、簡單佇列服務(Simple Queue Service, SQS)、靈活支付服務(Flexible Payments Service, FPS)和SimpleDB來構建網絡規模的商業應用程序。透過AWS,亞馬遜提供了一種新的IT基礎設施範式:根據需要使用,隨需而用,按使用量付費。《編程網絡服務》解釋了如何訪問亞馬遜的開放API來存儲和運行應用程序,而不是浪費寶貴的時間和資源來構建自己的系統。通過本書,您將學到所有技術細節,以便:
- 使用應用程序伺服器、無限數據存儲和帶寬,通過亞馬遜S3服務存儲和檢索任意數量的數據
- 使用亞馬遜EC2的介面購買計算時間,請求機器,載入應用程序環境,管理訪問權限,並根據需要運行您的映像
- 使用亞馬遜SQS利用網絡規模的消息基礎設施來存儲在計算機之間傳遞的消息
- 利用亞馬遜FPS服務結構化支付指令,允許資金在任何兩個實體之間(人或計算機)流動
- 創建和存儲多個數據集,輕鬆查詢數據並返回結果,使用亞馬遜SimpleDB。
隨時擴展或縮減,根據需要使用這些服務來使用所需的時間和空間。無論您是開始新的在線業務,需要擴展現有服務,還是需要為您的家庭提供異地備份,《編程網絡服務》都為您提供了開始使用AWS所需的背景和實用知識。其他書籍解釋了如何構建網絡服務。本書教導企業如何利用來自成熟技術領導者的現有服務。
**創建HTML POST表單,允許您的網站訪問者使用標準網頁瀏覽器將文件上傳到您的S3帳戶**
作者:James Murty,JetS3t Java S3庫的創建者及《編程亞馬遜網絡服務》的作者
亞馬遜的簡單存儲服務(S3)為任何擁有信用卡和亞馬遜網絡服務(AWS)帳戶的人提供便宜且無限的在線數據存儲。如果您擁有AWS帳戶,您可以使用專門的工具與S3服務互動,以上傳和管理您的文件。對於自己來說,訪問這個在線存儲資源非常方便,但可能會有一些情況,您希望允許其他人將文件上傳到您的帳戶中。
為此,S3通過特別設計和預先授權的HTML POST表單接受上傳。您可以在任何網頁中包含這些表單,以允許您的網站訪問者僅使用標準網頁瀏覽器向您發送文件。
在這篇文章中,我將演示如何構建簡單的S3 POST表單。我假設您已經註冊了S3服務,並且擁有一個S3客戶端程序來創建存儲桶和查看您帳戶中的文件。在您繼續之前,請創建自己的存儲桶以存儲上傳的文件——在下面的示例中,我將使用名為* s3-bucket *的存儲桶。
**POST表單網頁**
這是一個包含S3 POST表單的網頁,您可以將其用作自己表單的模板:
```html
S3 POST Form
文件上傳到S3:
```
這個模板展示了S3 POST表單的一些重要特徵,以及包含它的網頁:
- 包含表單的網頁在部分有一個標籤,告訴網頁瀏覽器使用UTF-8
Unicode字符編碼。
- 表單的action
參數指定了一個S3 URL,其中包含您的目標存儲桶的名稱,在這種情況下是名為* s3-bucket *的存儲桶。
- 表單包含多個輸入字段,這些字段的參數名稱和值將發送到S3服務。如果缺少任何必需的輸入字段,或者某個字段的值不正確,該服務將不接受來自該表單的上傳。
S3服務使用表單輸入字段中的信息來授權上傳,並設置上傳文件對象的屬性。以下是最常見的輸入字段的描述:
| 字段名稱 | 描述 |
| --- | --- |
| key | 用於存儲上傳文件數據的S3對象的名稱。當您知道用戶將上傳什麼信息時,可以提前設置此名稱,例如:* uploads/monthly_report.txt *。
如果您不知道用戶將上傳的文件名稱,key值可以包含特殊變量${filename}
,該變量將被上傳文件的名稱替換。例如,key值* uploads/${filename} *將變成對象名稱* uploads/Birthday Cake.jpg *,如果用戶上傳了一個名為* Birthday Cake.jpg *的文件。 |
| AWSAccessKeyId | 您的亞馬遜網絡服務帳戶的訪問密鑰標識符憑證。 |
| acl | 應用於上傳文件的訪問控制策略。如果您不希望上傳的文件對公眾可用,則應使用值private
。要使上傳的文件公開可用,請使用值public-read
。 |
| success_action_redirect | 用戶的網頁瀏覽器在文件上傳後將被重定向的URL地址。此URL應指向您網站上的「成功上傳」頁面,以便您可以告知用戶他們的文件已被接受。S3將向此URL值添加bucket
、key
和etag
參數,以告知您的網頁應用程序上傳文件的位置和哈希值。 |
| policy | 一個Base64編碼的政策文檔,對通過S3 POST表單發送的文件上傳施加規則。此文檔用於授權表單,並對可以上傳的文件施加條件。政策文檔將在下面更詳細地描述。 |
| signature | 授權表單的簽名值,證明只有您能創建它。此值是通過使用您的AWS秘密密鑰簽名Base64編碼的政策文檔計算的,這一過程我將在下面演示。 |
| Content-Type | 將應用於上傳文件的內容類型(mime類型),例如image/jpeg
用於JPEG圖片文件。如果您不知道用戶將上傳什麼類型的文件,您可以提示用戶提供適當的內容類型,或者編寫瀏覽器腳本代碼,自動根據文件名稱設置此值。
如果您不使用此字段設置內容類型,S3將使用默認值application/octet-stream
,這可能會導致某些網頁瀏覽器無法正確顯示文件。 |
| file | 允許用戶選擇要上傳的文件的輸入字段。此字段必須是表單中的最後一個字段,因為S3將忽略其下方的任何字段。 |
這個表單輸入字段的概述應該能幫助您修改模板POST表單以適應自己的目的。至少,您需要編輯表單的action
參數,以指向您自己的S3存儲桶,並將AWSAccessKeyId
字段的值設置為您的AWS訪問密鑰憑證。
要完成表單並使其可被S3服務接受,您還需要生成一個政策文檔和簽名值。
**政策文檔**
S3 POST表單包括一個授權表單並對可以上傳的文件施加限制的政策文檔。當S3通過POST表單接收文件時,它將檢查政策文檔和簽名,以確認該表單是由被允許在目標S3帳戶中存儲文件的人創建的。
政策文檔是一組以JavaScript對象表示法表達的屬性,這意味著文檔的結構和內容必須符合某種格式。每個政策文檔包含兩個頂級項目:
- expiration
- 一個格林威治標準時間(GMT)時間戳,指定政策文檔何時過期。一旦政策文檔過期,上傳表單將不再有效。
- conditions
- 一組規則,用於定義可以包含在表單輸入字段中的值,並對文件上傳施加大小限制。
以下是與上述POST表單模板相對應的政策文檔。此政策的expiration
日期為2009年1月1日:
```json
{'expiration': '2009-01-01T00:00:00Z',
'conditions': [
{'bucket': 's3-bucket'},
['starts-with', '$key', 'uploads/'],
{'acl': 'private'},
{'success_action_redirect': 'http://localhost/'},
['starts-with', '$Content-Type', ''],
['content-length-range', 0, 1048576]
]}
```
要創建有效的S3 POST表單,您必須包括一個政策文檔,其conditions
部分包含幾乎每個輸入字段的規則。至少,該文檔必須包括上傳文件對象的bucket
和key
值的規則。除了這兩條規則外,您還需要為表單中的每個其他輸入字段包含一條規則,除了AWSAccessKeyId
、signature
、policy
和file
。
由於我們的模板POST表單包括輸入字段acl
、success_action_redirect
和Content-Type
,因此我們的政策文檔包括對應於這些字段的規則。我們的政策文檔還包括一條額外的content-length-range
規則,限制可以上傳的文件大小。
您可以在政策文檔中應用三種規則:
1. 等式規則,檢查輸入字段的值是否設置為給定字符串。等式規則以名稱和值對的形式表示在大括號字符內,例如:{'acl': 'private'}
2. 開頭規則,檢查輸入字段的值是否以給定字符串開頭。如果給定字符串為空,S3將僅檢查該字段是否存在於表單中,而不關心其包含的值。開頭規則以三元素數組的形式表示。