如何使用aws-sdk和Nativescript将图像文件上传到Amazon S3

时间:2019-05-23 19:34:22

标签: image amazon-s3 nativescript nativescript-angular

我正在尝试使用aws-sdk将图像本地PNG上载到Amazon S3,但是到达Bucket的文件是双字节,并且已损坏。
我可以列出文件,创建存储桶等。


本机语言:5.2.4
npm:6.4.1
节点:10.15.3

我尝试使用方法file.readText()读取

 await file.readText().then(
    (content) => {
        this.binarySource = content;
    });

初始化Amazon S3

        AWS.config.update({
            region: "us-east-1",
            credentials: {
                accessKeyId: this.chaveAmazonS3,
                secretAccessKey: this.tokenAmazonS3
            }
        });
        const s3 = new AWS.S3({
            apiVersion: "2006-03-01"
        });
        this.amazonS3 = s3;

获取文件并使用putObject上传

        let artefato = '/data/user/0/org.nativescript.aixlab/files/midias/41_000014_1558633914086.png';
        let diretorioS3 = 'dirbucket/';

        let filename = artefato.substring(artefato.lastIndexOf(separator) + 1);
        filename = diretorioS3 + filename;

        const file: File = File.fromPath(artefato);
        this.binarySource = await file.readTextSync();
        // await file.readText().then(
        //     (content) => {
        //         this.binarySource = content;
        //     });

        console.log("filename", filename, file.size, this.binarySource.length, artefato);

        let params = {
            Bucket: this.bucketAmazonS3,
            Key: filename,
            Body: this.binarySource,
            ContentLength: this.binarySource.length,
            ContentType: 'image/png',
        };

        try {
            //let options = {partSize: 10 * 1024 * 1024, queueSize: 1};
            let response = await this.amazonS3.putObject(params, function (err, data) {
                console.log("PUT", err, data);
            }); 
        } catch (error) {
            console.error("Erro putObjectBucket", error);
            return false;
        }

控制台结果

JS: 
JS: PUT null {
JS:   "ETag": "\"913b7bda195f7bebfdaff5e5b10138a0\""
JS: }

这是File(模块)的值,并返回file.readTextSync


file.size = 193139
this.binarySource.length = 182599


Amazon Bucket中的对象
342.6 KB

我希望Amazon S3存储桶中的PNG文件为二进制格式。

由于其他旧版应用程序,我无法以base64格式保存。

0 个答案:

没有答案