5

目前,我正在使用@google-cloud/storage NPM軟件包將文件直接上傳到Google雲端存儲存儲分區。這需要一些詭計,因爲我只有圖像的base64編碼字符串。我必須:如何使用Node.js將base64編碼圖像(字符串)直接上傳到Google Cloud Storage存儲桶?

  • 解碼字符串
  • 另存爲文件
  • 發送的文件路徑下面的腳本上傳到谷歌雲存儲
  • 刪除本地文件

我想避免將文件存儲在文件系統中,因爲我正在使用Google App Engine,並且如果刪除操作因任何原因而不起作用,我不想超載文件系統/將垃圾文件放在那裏。這是我上傳腳本看起來像現在:

// Convert the base64 string back to an image to upload into the Google Cloud Storage bucket 
var base64Img = require('base64-img'); 
var filePath = base64Img.imgSync(req.body.base64Image, 'user-uploads', 'image-name'); 

// Instantiate the GCP Storage instance 
var gcs = require('@google-cloud/storage')(), 
    bucket = gcs.bucket('google-cloud-storage-bucket-name'); 

// Upload the image to the bucket 
bucket.upload(__dirname.slice(0, -15) + filePath, { 
    destination: 'profile-images/576dba00c1346abe12fb502a-original.jpg', 
    public: true, 
    validation: 'md5' 
}, function(error, file) { 

    if (error) { 
     sails.log.error(error); 
    } 

    return res.ok('Image uploaded'); 
}); 

反正是有直接上傳編碼的圖像,而不必把它轉換成一個文件,然後上傳使用路徑的字符串的base64?

+0

'bucket.upload'封裝了''文件。createWriteStream'函數,因此您需要將base64文件字符串傳送到由'file'中的該方法創建的流中。我建議只需寫入文件系統並在上傳後取消鏈接。我認爲你在刪除文件時會遇到問題。如果你真的喜歡,我可能會舉一個例子。 – forrestmid

+0

@forrestmid真的很感激你如何實現'file.createWriteStream'直接上傳的例子。謝謝! – Nag

回答

13

我相信這個解決方案是使用bucket.upload函數在Google Cloud Node SDK中包裝的file.createWriteStream功能。

我對流的經驗很少,所以如果這不起作用,請儘量忍受。

首先,我們需要將base64數據放入流中。爲此,我們將包含stream庫,從base64數據創建一個緩衝區,並將緩衝區添加到流的末尾。

var stream = require('stream'); 
var bufferStream = new stream.PassThrough(); 
bufferStream.end(new Buffer(req.body.base64Image, 'base64')); 

更多關於decoding base64creating the stream

然後,我們將把流傳輸到由file.createWriteStream函數創建的寫入流中。

var gcs = require('@google-cloud/storage')({ 
    projectId: 'grape-spaceship-123', 
    keyFilename: '/path/to/keyfile.json' 
}); 

//Define bucket. 
var myBucket = gcs.bucket('my-bucket'); 
//Define file & file name. 
var file = myBucket.file('my-file.jpg'); 
//Pipe the 'bufferStream' into a 'file.createWriteStream' method. 
bufferStream.pipe(file.createWriteStream({ 
    metadata: { 
     contentType: 'image/jpeg', 
     metadata: { 
     custom: 'metadata' 
     } 
    }, 
    public: true, 
    validation: "md5" 
    })) 
    .on('error', function(err) {}) 
    .on('finish', function() { 
    // The file upload is complete. 
    }); 

信息上file.createWriteStreamFile docsbucket.uploadbucket.upload method code in the Node SDK

所以上述代碼的工作方式是定義你想放入文件的桶,然後定義文件和文件名。我們在這裏不設置上傳選項。然後,我們將我們剛剛創建的bufferStream變量輸入到我們之前討論的file.createWriteStream方法中。在這些選項中,我們定義了要實現的元數據和其他選項。直接查看Node code on Github以瞭解它們如何分解bucket.upload函數非常有用,並且建議您也這樣做。最後,我們在上傳完成時以及錯誤發生時附加一些事件。

+0

謝謝發佈!我實際上做了類似的事情,除了我使用[file.save()](https://googlecloudplatform.github.io/google-cloud-node/#/docs/storage/0.8.0/storage/file?method=保存)API,它是'createWriteStream'的一個包裝。 – Nag

+1

@Nag這絕對有用!我通讀了這個API,但沒有注意到它的操作與你正在尋找的相匹配。很高興你能弄明白。 – forrestmid

+0

@Nag你是如何設法做到這一點的?你有源代碼,我們可以看看?我正在爲此苦苦掙扎。我試圖從Firebase雲端功能上傳base64編碼的圖像字符串到Firebase存儲 – krlozadan

1

發佈我的版本答案的響應上述@krlozadan的要求:

// Convert the base64 string back to an image to upload into the Google Cloud Storage bucket 
var mimeTypes = require('mimetypes'); 

var image = req.body.profile.image, 
    mimeType = image.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/)[1], 
    fileName = req.profile.id + '-original.' + mimeTypes.detectExtension(mimeType), 
    base64EncodedImageString = image.replace(/^data:image\/\w+;base64,/, ''), 
    imageBuffer = new Buffer(base64EncodedImageString, 'base64'); 

// Instantiate the GCP Storage instance 
var gcs = require('@google-cloud/storage')(), 
    bucket = gcs.bucket('my-bucket'); 

// Upload the image to the bucket 
var file = bucket.file('profile-images/' + fileName); 

file.save(imageBuffer, { 
    metadata: { contentType: mimeType }, 
    public: true, 
    validation: 'md5' 
}, function(error) { 

    if (error) { 
     return res.serverError('Unable to upload the image.'); 
    } 

    return res.ok('Uploaded'); 
}); 

這對我的工作就好了。忽略前幾行中的一些附加邏輯,因爲它們只與我正在構建的應用程序有關。

相關問題