Skip to content

To save stream file directly to GoogleCloudStorage via GoogleCloudFunction #513

@geoseong

Description

@geoseong

I want to save stream data directly to GoogleCloudStorage via GoogleCloudFunction Node.js.

I tried some ways but works nothing.
Now i'm trying at localhost server first.

so once I tried to test, this error shown :
req.file.cloudStorageError = err;
TypeError: Cannot set property 'cloudStorageError' of undefined

I want to know how this works and get some clue.. plz help me with do this.

client side code

HTML

    <form data-brackets-id='37' action="http://localhost:8989/fileupload" method="post" enctype="multipart/form-data" name="">
        <input data-brackets-id='38' type="file" name="file" id="file" value="">
        <input data-brackets-id='39' type="button" data-inline="true" value="Input" id="filesubmit">
    </form>

Script

    $('form #filesubmit').on('click', (e)=>{
        var uploadfile = $("input[name=file]")[0].files[0];
        var formData = new FormData(); 
        formData.append("myfile", $("input[name=file]")[0].files[0]); 
        
        console.log('uploadfile', uploadfile, uploadfile.type);
       
        var googleCloud = 'https://us-central1-xxxx-xxxxx.cloudfunctions.net/functionNm';
        var localurl = 'http://localhost:8989/fileupload'
        $.ajax({ 
            url: localurl,  // googleCloud
            data: formData, 
            processData: false, 
            contentType: false, 
            type: 'POST', 
            success: function(data){ 
                console.log('response data', data);
            } 
        });

    });

server side code

// start form
var Storage = require('@google-cloud/storage');
var multiparty = require('multiparty');
var fs = require('fs');
var form = new multiparty.Form();

const storage = Storage({
    projectId: [My Project ID]
});
const bucket = storage.bucket('multilang_service');

function getPublicUrl (filename) {
  return `https://storage.googleapis.com/${CLOUD_BUCKET}/${filename}`;
}

// get field name & value
form.on('field',function(name,value){
  console.log('[?] normal field / name = '+name+' , value = '+value);
});

// file upload handling
form.on('part',function(part){
    var filename;
    var size;
    if (part.filename) {
        filename = part.filename;
        size = part.byteCount;
    }else{
        part.resume();
    }   
    
    const gcsname = Date.now() + filename;
    const file = bucket.file(gcsname);
    const stream = file.createWriteStream({
        metadata: {
          contentType: part.headers['content-type']
        }
    });
    stream.on('error', (err) => {
        req.file.cloudStorageError = err;
        next(err);
    });

    stream.on('finish', () => {
        req.file.cloudStorageObject = gcsname;
        file.makePublic().then(() => {
          req.file.cloudStoragePublicUrl = getPublicUrl(gcsname);
          next();
        });
    });
    stream.end(form);
    
    part.on('data',function(chunk){
        console.log('[2] '+filename+' read '+chunk.length + 'bytes');
    });

    part.on('end',function(){
        console.log('[4] '+filename+' Part read complete');
    });
});

// track progress
form.on('progress',function(byteRead,byteExpected){
    console.log('[3] Reading total  '+byteRead+'/'+byteExpected);
});

// all uploads are completed
form.on('close',function(){
    res.send('done for test');
});

form.parse(req);
// end form

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions