How to get a raw response like XML using s3 client from aws/sdk-javascript.

0

When I execute ListBucketscommand() using the SDK's S3 client, you can get the following result. How to get a raw response like XML using s3 client from aws/sdk.

// sample code & result const command = new ListBucketsCommand({}); const response = await client.send(command); console.log(response); '$metadata': { httpStatusCode: 200, requestId: '1759FE4B14A997E0', extendedRequestId: 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', cfId: undefined, attempts: 1, totalRetryDelay: 0 }, Buckets: [ { Name: 'test', CreationDate: 2023-03-13T04:45:29.702Z }, { Name: 'test-0424', CreationDate: 2023-04-24T03:25:08.831Z }, ]

// xml format

<?xml version="1.0" encoding="UTF-8"?> <ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <Owner> <DisplayName>minio</DisplayName> <ID>02d6176db174dc93cb1b899f7c6078f08654445fe8cf1b6ce98d8855f66bdbf4</ID> </Owner> <Buckets> <Bucket> <Name>test</Name> <CreationDate>2023-03-13T04: 45: 29.702Z</CreationDate> </Bucket> <Bucket> <Name>test-0424</Name> <CreationDate>2023-04-24T03: 25: 08.831Z</CreationDate> </Bucket> </Buckets> </ListAllMyBucketsResult>
preguntada hace un año1008 visualizaciones
2 Respuestas
1

The SDK parses the raw XML responses from the S3 API and converts them into JavaScript objects before returning them to the user.

https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-select.html

But you can directly query S3 API

https://docs.aws.amazon.com/AmazonS3/latest/API/API_SelectObjectContent.html

You can use axios library to do this.

profile picture
EXPERTO
respondido hace un año
1

For AWS SDK for JavaScript v2, you can access the raw response via the response field on the request object. Note that response field on the request object will not be set until after the request has been completed, thus the need to await in the example below before accessing.

const listBucketsRequest = S3.listBuckets({});
const listBucketsResponse = await listBucketsRequest.promise()
console.log(listBucketsRequest.response.httpResponse.body.toString());

If AWS SDK for JavaScript v3 does not expose a similar set of objects. Access to the raw response would be done at the middleware level:

profile pictureAWS
respondido hace un año

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas