简体   繁体   中英

read text file aws-amplify s3 read data from presigned URL

How can I read a text file in s3 using the aws-amplify library? I am able to get the presigned URL but get an error when going to the url,

<Error>
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
<Key>
private/xx-xxxx-4:42134231243423142131423/filename.json
</Key>
<RequestId>1242341243</RequestId>
<HostId>
 uudawfeawefhszec/6kM8VawefawefawfawefawefgwegweawgeagweyRk=
</HostId>
</Error>

The Documentation has instructions on how to get the url:

Storage.get('test.txt', {level: 'private'})
  .then(result => console.log(result))
  .catch(err => console.log(err));

The result is the url to the path, not the text. When I run a http get method, the endpoint does not exist.

What's the best practice to fetch the data? I'm using react.

If there's a method such as Storage.getText(...) that would be best case scenario.

I've tried adding authorization using this...

  Auth.currentCredentials()
            .then(credentials => {
                axios.get(result, { headers: { 'Authorization': JSON.stringify(Auth.essentialCredentials(credentials)) } })
                    .then(r => {

                        console.log(r);
                    })
            });

400 Bad Request :(

Looking at the source code, the S3Image class uses an undocumented parameter in the Storage.get method

Storage.get('...', {download: true}) 

is the correct way to get the object.

The object will need to be decoded

    Storage.get('modules.json', { download: true })
        .then(result => {
            console.log(Utf8ArrayToStr(result.Body));
        })
        .catch(err => {
            console.log('error axios');
            console.log(err)
        });

function Utf8ArrayToStr(array) {
    var out, i, len, c;
    var char2, char3;

    out = "";
    len = array.length;
    i = 0;
    while (i < len) {
        c = array[i++];
        switch (c >> 4) {
            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                // 0xxxxxxx
                out += String.fromCharCode(c);
                break;
            case 12: case 13:
                // 110x xxxx   10xx xxxx
                char2 = array[i++];
                out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
                break;
            case 14:
                // 1110 xxxx  10xx xxxx  10xx xxxx
                char2 = array[i++];
                char3 = array[i++];
                out += String.fromCharCode(((c & 0x0F) << 12) |
                    ((char2 & 0x3F) << 6) |
                    ((char3 & 0x3F) << 0));
                break;
        }
    }

    return out;
}

Updates to the Amplify API have now provided easier ways to achieve this.

const result = await Storage.get(`filename.txt`, { contentType: 'text/plain', download: true });

// data.Body is a Blob
result.Body.text().then(string => { 
  // handle the String data return String 
  console.log(string);
});

Storage.get - File download option

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM