JavaScript ES6Sintaxis /CommonJS - AWS SDK para JavaScript

La Guía de referencia de la API de AWS SDK for JavaScript V3 describe en detalle todas las operaciones de la API para la versión 3 (V3) de AWS SDK for JavaScript.

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

JavaScript ES6Sintaxis /CommonJS

Los ejemplos AWS SDK para JavaScript de código están escritos en ECMAScript 6 ()ES6. ES6 incorpora nueva sintaxis y nuevas funciones para hacer que su código sea más moderno y legible, y haga más.

ES6 requiere que utilice la versión 13.x o superior de Node.js. Para descargar e instalar la versión más reciente de Node.js, consulte Descargas de Node.js. No obstante, si lo prefiere, puede convertir cualquiera de nuestros ejemplos a la sintaxis CommonJS. Para ello, siga las instrucciones que se indican a continuación:

  • Elimine module de package.json del entorno de su proyecto.

  • Convierta todas las ES6 import sentencias en sentencias CommonJSrequire. Por ejemplo, convierta:

    import { CreateBucketCommand } from "@aws-sdk/client-s3"; import { s3 } from "./libs/s3Client.js";

    A su equivalente en CommonJS:

    const { CreateBucketCommand } = require("@aws-sdk/client-s3"); const { s3 } = require("./libs/s3Client.js");
  • Convierte todas las ES6 export declaraciones en declaraciones de CommonJSmodule.exports. Por ejemplo, convierta:

    export {s3}

    A su equivalente en CommonJS:

    module.exports = {s3}

El siguiente ejemplo muestra el ejemplo de código para crear un bucket de Amazon S3 tanto en CommonJS como ES6 en CommonJS.

ES6

libs/s3Client.js

// Create service client module using ES6 syntax. import { S3Client } from "@aws-sdk/client-s3"; // Set the AWS region const REGION = "eu-west-1"; //e.g. "us-east-1" // Create Amazon S3 service object. const s3 = new S3Client({ region: REGION }); // Export 's3' constant. export {s3};

s3_createbucket.js

// Get service clients module and commands using ES6 syntax. import { CreateBucketCommand } from "@aws-sdk/client-s3"; import { s3 } from "./libs/s3Client.js"; // Get service clients module and commands using CommonJS syntax. // const { CreateBucketCommand } = require("@aws-sdk/client-s3"); // const { s3 } = require("./libs/s3Client.js"); // Set the bucket parameters const bucketParams = { Bucket: "BUCKET_NAME" }; // Create the Amazon S3 bucket. const run = async () => { try { const data = await s3.send(new CreateBucketCommand(bucketParams)); console.log("Success", data.Location); return data; } catch (err) { console.log("Error", err); } }; run();
CommonJS

libs/s3Client.js

// Create service client module using CommonJS syntax. const { S3Client } = require("@aws-sdk/client-s3"); // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create Amazon S3 service object. const s3 = new S3Client({ region: REGION }); // Export 's3' constant. module.exports ={s3};

s3_createbucket.js

// Get service clients module and commands using CommonJS syntax. const { CreateBucketCommand } = require("@aws-sdk/client-s3"); const { s3 } = require("./libs/s3Client.js"); // Set the bucket parameters const bucketParams = { Bucket: "BUCKET_NAME" }; // Create the Amazon S3 bucket. const run = async () => { try { const data = await s3.send(new CreateBucketCommand(bucketParams)); console.log("Success", data.Location); return data; } catch (err) { console.log("Error", err); } }; run();