AWS Miscellaneous

API gateway usage plan

  • What is a usage plan
    • Usage plans allow access to one or more deployed API stages that enforce configurable limits and quota limits on individual client API keys.
    • API callers are identified by API keys that can be generated by API Gateway or imported from external sources.
    • Notice:
      • Limits and quota limits apply to requests for individual API keys aggregated across all API stages within a usage plan.
  • Configure usage plans

    • 1. Create one or more APIs, configure the methods to require an API key, and deploy the APIs at various stages.

    • 2. Use your API to generate an API key and distribute the key to application developers (your customers).

    • 3. Create a usage plan with desired limits and quota limits.

    • 4. The selected API stage and API key are associated with the usage plan.


JS Link AWS

  • You can hardcode credentials by passing credential information to the configuration object using AWS.config.update() :
AWS.config.update({accessKeyId: 'akid', secretAccessKey: 'secret'});
  • Configure CORS for Amazon S3 Bucket
    • A quick example of CORS configuration is shown below. This example allows users to view, add, delete or update objects inside the bucket from any external domain, but it is recommended to put the "AllowedOrigin" scope to the domain your website is running on (you can specify "*" to allow any origin).
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <CORSRule>
    <AllowedOrigin>https://example.org</AllowedOrigin>
    <AllowedMethod>HEAD</AllowedMethod>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>DELETE</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
    <ExposeHeader>ETag</ExposeHeader>
    <ExposeHeader>x-amz-meta-custom-header</ExposeHeader>
  </CORSRule>
</CORSConfiguration>

JS S3 upload file configuration details

  • Send request to S3
var s3 = new AWS.S3();
s3.abortMultipartUpload(params, function (err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});
  • Lock API version
    • To ensure that the S3 object uses this particular API, the object can be constructed by passing the apiVersion option to the constructor:
var s3 = new AWS.S3({apiVersion: '2006-03-01'});

/*
//您还可以AWS.config.apiVersions使用s3服务标识符全局设置API版本:
AWS.config.apiVersions = {
  s3: '2006-03-01',
  // other service API versions
};
var s3 = new AWS.S3();
*/
  • Construction method
    • new AWS.S3 (options =}})
    • eg:
var s3 = new AWS.S3({
        apiVersion: '2006-03-01',
        params: {Bucket: albumBucketName}
    });

  • Upload method: upload()
    • (AWS.S3.ManagedUpload) upload(params = {}, [options], [callback])
    • If the payload is large enough, the intelligent concurrent processing part can be used to upload buffers, blobs or streams of any size. You can configure the concurrent queue size by setting options. Note that this is the only action the SDK can retry requests with fluid.
    • example:
  • upload stream object
var params = {Bucket: 'bucket', Key: 'key', Body: stream};
s3.upload(params, function(err, data) {
  console.log(err, data);
});
  • Upload a data stream with a concurrency of 1 and a partSize of 10mb
var params = {Bucket: 'bucket', Key: 'key', Body: stream};
var options = {partSize: 10 * 1024 * 1024, queueSize: 1};
s3.upload(params, options, function(err, data) {
  console.log(err, data);
});
parameter:
  • ACL - (String) The ACL that can be used to apply to the object. Possible values ​​include:
    • ”private”
    • “public-read”
    • “public-read-write”
    • “authenticated-read”
    • “aws-exec-read”
    • “bucket-owner-read”
    • “bucket-owner-full-control”
  • Body - (Buffer, Typed Array, Blob, String, ReadableStream) object data.
  • Bucket - (String) The name of the bucket to initiate the PUT operation.
  • CacheControl - (String) Specifies the caching behavior in the request/reply chain.
  • ContentDisposition - (String) Specifies the representation information of the object.
  • ContentEncoding — (String) Specifies what content encodings have been applied to the object and thus what decoding mechanisms must be applied to obtain the media-type referenced by the Content-Type header field.
  • ContentLanguage - (String) The language in which the content is located.
  • ContentLength - (Integer) The size of the body (in bytes) This parameter is useful when the size of the body cannot be determined automatically.
  • ContentMD5 - (String) The base64 encoded 128-bit MD5 digest of the partial data.
  • ContentType - (String) Standard MIME type describing the format of the object data.
  • Expires - (Date) The date and time the object is no longer cacheable.
  • GrantFullControl - (String) Grants READ, READ_ACP and WRITE_ACP permissions to the object's grantee.
  • GrantRead- (String) allows the grantee to read the object data and its metadata.
  • GrantReadACP - (String) Allows the grantee to read the object ACL.
  • GrantWriteACP - (String) Allows the grantee to write the ACL for the applicable object.
  • Key - (String) The object key that initiates the PUT operation.
  • Metadata - (map) Stores a map to the object's metadata in S3.
  • ServerSideEncryption - (String) The server-side encryption algorithm to use when storing this object in S3 (eg, AES256, aws:kms). Possible values ​​include:
    • ”AES256”
    • “aws:kms”
  • StorageClass - (String) The storage type used for the object. Defaults to "Standard". Possible values ​​include:
    • ”STANDARD”
    • “REDUCED_REDUNDANCY”
    • “STANDARD_IA”
  • WebsiteRedirectLocation - (String) If the bucket is configured as a website, redirect requests for this object to another object in the same bucket or to an external URL. Amazon S3 stores the value of this header in the object metadata.
  • SSECustomerAlgorithm - (String) Specifies the algorithm to use when encrypting the object (for example, AES256).
  • SSECustomerKey - (Buffer, Typed Array, Blob, String) Specifies the customer-provided encryption key used for Amazon S3 encrypted data. The value is used to store the object and is then discarded; Amazon does not store encryption keys. The key must be suitable for use with the algorithm specified in the x-amz-server-side-encryption-customer-algorithm header.
  • SSECustomerKeyMD5 - (String) Specifies the 128-bit MD5 digest of the encryption key according to RFC 1321. Mas S3 uses this header for message integrity checking to ensure that the encryption key is transmitted error-free.
  • SSEKMSKeyId - (String) Specifies the AWS KMS key ID used for object encryption. All GET and PUT requests to objects secured by AWS KMS will fail if not over SSL or using SigV4
return:
  • (AWS. S3. ManagedUpload) - A managed upload object that can call send() or track progress.
(AWS.S3.ManagedUpload) — the managed upload object that can call send() or track progress.
Callback:
function(err, data) { ... }
  • Parameters:
    • err (Error) — An error, or null if no error occurred.
    • data (map) — The response data from the successful upload: * Location (String) the URL of the uploaded object * ETag (String) the ETag of the uploaded object * Bucket (String) the bucket to which the object was uploaded * Key (String) the key to which the object was uploaded//response data for successful upload: * Location (String) URL of the uploaded object * (String) ETag of the uploaded object ETag * Bucket (String) The bucket of the uploaded object * Key ( String) object of the object has been uploaded

Case Studies:

/**
 * 文件上传
 * @param albumName
 */
function addFile(albumName) {
    var albumBucketName = '**********';
    var bucketRegion = 'us-east-1';
    var IdentityPoolId = '********************';//用户池编号

    AWS.config.update({
        region: bucketRegion,
        credentials: new AWS.CognitoIdentityCredentials({
            IdentityPoolId: IdentityPoolId
        })
    });

    var s3 = new AWS.S3({
        apiVersion: '2006-03-01',
        params: {Bucket: albumBucketName}
    });
    var files = document.getElementById('file1').files;
    if (!files.length) {
        return alert('Please choose a file to upload first.');
    }

    var file = files[0];
    var fileName = file.name;
    alert(fileName);
    var albumPhotosKey = encodeURIComponent(albumName) + '/';
    alert(albumPhotosKey);
    var photoKey = albumPhotosKey + fileName;
    s3.upload({
        Key: photoKey,
        Body: file,
        ACL: 'public-read'
    }, function (err, data) {
        if (err) {
            return alert('There was an error uploading your photo: ', err.message);
        }
        alert('Successfully uploaded photo.');
    });
}

Build SDKs with SDK Builder

  • The easiest way to create your own build of the AWS SDK for JavaScript is to use the SDK Builder web application https://sdk.amazonaws.com/builder/js . Use the SDK builder to specify the services and their API versions to include in the build.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326001616&siteId=291194637
Recommended