当前位置 : 主页 > 手机开发 > cordova >

从Cordova app将文件流式传输到服务器

来源:互联网 收集:自由互联 发布时间:2021-06-10
我有cordova应用程序,用户必须能够从手机中选择一个图像并将其上传到AWS S3.我正在使用 cordova-plugin-camera和S3 SDK.我的代码工作正常(见下文),但需要将整个图像拉入base64编码字符串内的内
我有cordova应用程序,用户必须能够从手机中选择一个图像并将其上传到AWS S3.我正在使用 cordova-plugin-camera和S3 SDK.我的代码工作正常(见下文),但需要将整个图像拉入base64编码字符串内的内存(每 Camera.DestinationType.DATA_URL),然后再次进入缓冲区.

我希望通过手机的硬盘驱动器流式传输,以缓解潜在的内存问题. cordova-plugin-camera网站甚至对此有警告:

/** * Warning: Using DATA_URL is not recommended! The DATA_URL
destination * type is very memory intensive, even with a low quality
setting. Using it * can result in out of memory errors and
application crashes. Use FILE_URI * or NATIVE_URI instead. */

那么有没有办法使用FILE_URI或NATIVE_URI或任何其他技术将照片直接从磁盘流式传输到S3?

这是我目前的代码:

doTheThing() {
    let options = {
        quality: 50,
        destinationType: Camera.DestinationType.DATA_URL,
        sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
        encodingType: Camera.EncodingType.JPG
    };

    navigator.camera.getPicture(imgData => {
       this.uploadToS3(imgData);
    }, options);


    uploadToS3(data) {
        let AWS = require('aws-sdk');

        AWS.config = new AWS.Config();
        AWS.config.accessKeyId = 'noway';
        AWS.config.secretAccessKey = 'jose';
        AWS.config.region = 'us-east-2';

        let s3 = new AWS.S3();
        let uploadParams = { Bucket: 'iamkule/f1/f2', Key: '', Body: new Buffer(data, 'base64') };
        uploadParams.Key = 'myfile.jpg';

        s3.upload(uploadParams, function (err, data) {
           if (data) {
              console.log('yay!');
           }
        });
    }
}
这应该让你开始.请原谅我的伪代码.随意根据需要进行编辑.

设备将映像保存到磁盘:

function takePicture(urlCallback){
    let options = {
        quality: 50,
        destinationType: Camera.DestinationType.FILE_URI,// <--
        sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
        encodingType: Camera.EncodingType.JPG
    };
    navigator.camera.getPicture(urlCallback, onFail, options);  
    function onFail(message) {
        alert('Failed because: ' + message);
    }
}

从磁盘获取图像为blob:

function getImageAsBlob(url, blobCallback) {
    var xhr = new XMLHttpRequest();
    xhr.open( "GET", url, true );
    xhr.responseType = "arraybuffer";
    xhr.onload = function( ev ) {
        // Obtain a blob: URL for the image data.
        var arrayBufferView = new Uint8Array( this.response );
        var blob = new Blob( [ arrayBufferView ], { type: "image/jpeg" } );
        blobCallback(blob);
    };
    xhr.send();
}

将blob上传到S3:

function uploadToS3(blob, callback) {
    let AWS = require('aws-sdk');
    AWS.config = new AWS.Config();
    AWS.config.accessKeyId = 'noway';
    AWS.config.secretAccessKey = 'jose';
    AWS.config.region = 'us-east-2';
    let s3 = new AWS.S3();
    let options = { Bucket: 'iamkule/f1/f2', Key: 'myfile.jpg', Body: blob };// <--
    s3.upload(options, callback);
}

把它们放在一起:

takePicture(function(url){
    getImageAsBlob(url, function(blob){
        uploadToS3(blob, function (err, data) {
            if (data) console.log('yay!');
        });
    });
});

S3信息来自here.照片捕获信息来自here. 快乐的编码!

网友评论