Há mais exemplos do AWS SDK disponíveis no repositório do GitHub Documento de Exemplos do AWS SDK
Uma página da web que oferece uma lista de objetos do Amazon S3 usando um AWS SDK
O exemplo de código a seguir mostra como listar objetos do Amazon S3 em uma página da web.
- JavaScript
-
- SDK para JavaScript (v3)
-
nota
Há mais no GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository
. O código a seguir é o componente relevante do React que faz chamadas para o AWS SDK. Uma versão executável da aplicação que contém esse componente pode ser encontrada no link anterior do GitHub.
import { useEffect, useState } from "react"; import { ListObjectsCommand, type ListObjectsCommandOutput, S3Client, } from "@aws-sdk/client-s3"; import { fromCognitoIdentityPool } from "@aws-sdk/credential-providers"; import "./App.css"; function App() { const [objects, setObjects] = useState< Required<ListObjectsCommandOutput>["Contents"] >([]); useEffect(() => { const client = new S3Client({ region: "us-east-1", // Unless you have a public bucket, you'll need access to a private bucket. // One way to do this is to create an Amazon Cognito identity pool, attach a role to the pool, // and grant the role access to the 's3:GetObject' action. // // You'll also need to configure the CORS settings on the bucket to allow traffic from // this example site. Here's an example configuration that allows all origins. Don't // do this in production. //[ // { // "AllowedHeaders": ["*"], // "AllowedMethods": ["GET"], // "AllowedOrigins": ["*"], // "ExposeHeaders": [], // }, //] // credentials: fromCognitoIdentityPool({ clientConfig: { region: "us-east-1" }, identityPoolId: "<YOUR_IDENTITY_POOL_ID>", }), }); const command = new ListObjectsCommand({ Bucket: "bucket-name" }); client.send(command).then(({ Contents }) => setObjects(Contents || [])); }, []); return ( <div className="App"> {objects.map((o) => ( <div key={o.ETag}>{o.Key}</div> ))} </div> ); } export default App;-
Consulte detalhes da API em ListObjects na Referência da API AWS SDK para JavaScript.
-
Criar uma aplicação com tecnologia sem servidor para gerenciar fotos
Criar uma aplicação de exploração do Amazon Textract