2016-09-06 40 views
2

我爲我的後端API使用Strongloop loopback。 我正在使用它的loopback-component-storage來上傳和管理文件。更具體地說,我正在使用文件系統,但在生產時會切換到AWS存儲。Loopback:如何將文件從一個容器複製到另一個容器?

平臺上的用戶可以複製一個項目,爲此也將複製已上傳的鏈接文件。

我的問題是,如何從一個容器複製到另一個文件?

這裏是我到目前爲止的代碼來創建一個新的容器:

Project.app.models.storage.getContainer(templateId, function (err, container) { 
    if(err){ 
     // If error, no docs have been added or created, so just skip       
     return; 
    } 
    Project.app.models.storage.createContainer({ name: projectId},function (err,container) { 
    if(err){ 
     console.log("Error creating a container when copying project"); 
     return; 
    } 
    // Here is where I need assistance 
    }); 
}); 

回答

2

它可以創建下載流然後通過管道它從一個容器到另一個上載流,但不會是一個好主意因爲你會創建不必要的流量。讓你的底層環境爲你做好工作。

在您的本地開發環境,可以使用FS模塊處理文件,但是當您遷移存儲至S3你應該使用AWS SDK複製文件。您可以在您的存儲模型上創建遠程方法,它將採用源存儲桶/文件夾和目標存儲桶/文件夾爲您複製文件。

喜歡的東西:

Storage.remoteMethod(
    'copyS3File', 
    { 
     accepts: [ 
     {arg: 'copySource', type: 'string'}, 
     {arg: 'destinationBucket', type: 'string'}, 
     {arg: 'destinationFileName', type: 'string'}], 
     returns: {arg: 'success', type: 'string'} 
    } 
); 

    Storage.copyS3File = function(copySource, destinationBucket, destinationFileName, cb) { 
    var AWS = require('aws-sdk'); 
    var s3 = new AWS.S3({params: {Bucket: 'yourbucket',region:'yourregion'}}); 
    var params = {CopySource: copySource, Bucket: destinationBucket, Key: destinationFileName}; 
    s3.copyObject(params, function(err, success) { 
     if (err) cb(err); // an error occurred 
     else  cb(null, success)   // successful response 
    }); 
    }); 

Calling the copyObject operation的S3。

請注意,此代碼是不是測試或準備投產。這裏只是給你一個想法如何去做。

還要注意的是S3比loopbackjs實現存儲的有所不同。 S3具有扁平結構,但使用鍵名稱前綴支持文件夾中的文件組織。

Working with S3 folders

爲了讓事情變得更加清晰,可以通過該列表從源容器和循環獲取文件列表複製使用像上面這樣創建的遠程方法文件到新的目標:

Storage.getFiles({container: 'yourContainer'}, function(files){ 

    files.forEach(function(file){ 
    Storage.copyS3File(file.container + '/' + file.name, 'newContainer', 'newFile.name', cb); 
    }); 

}, function(err){ 
    console.log(err); 
}); 
1

的主意爲文件系統提供程序執行此操作

  1. 從要從中複製文件的容器中獲取文件列表,使用storage.GetFiles('container-name ',回調)
  2. 創建您要使用存儲複製這些文件的容器。 createContainer()
  3. 獲取流使用storage.uploadStream上傳(「recently-創建容器」,「文件名」)
  4. 獲取讀取流文件和管道的讀取和寫入操作
  5. 重複步驟3和4的每個文件(或者你可以鏈中的數據流,如果你想)

在你的情況的代碼看起來像

Project.app.models.storage.getContainer(templateId, function (err, container) { 
    if(err || !container){ 
     // If error, no docs have been added or created, so just skip 
     return; 
    } 
    Project.app.models.storage.getFiles(templateId, function (err, files) { 
     if(err){ 
      // If error, no docs have been added or created, so just skip 
      return; 
     } 
     Project.app.models.storage.createContainer({ name: projectId},function (err,newcontainer) { 
     if(err || !newcontainer){ 
      console.log("Error creating a container when copying project"); 
      return; 
     } 
     files.forEach(function(file){ 
      var writer = container.uploadStream(newcontainer.name, file.name); 
      require('fs').createReadStream('path-to-storage-directory/'+container.name+'/'+file.name).pipe(writer); 
      writer.on('finish', function(err,done){ 
       console.log('finished writing file'+file.name) 
      }); 
      writer.on('error', function(err){ 
       console.log('error in copying file'+file.name,err) 
      }); 
     }); 
     }); 
    }) 
}) 
相關問題