如何自动将文件夹上载到azure? [英] How to upload folder into azure automatically?

查看:81
本文介绍了如何自动将文件夹上载到azure?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我有了这部分代码,它向我显示了目录中的所有文件夹.

Now I have this part of a code, which shows me all folders in the directory.

        var dirPath = path.join("C:\\", 'ILJATEST');
    fs.readdir(dirPath, function (err, files) {
        if (err) {
            return console.log('Unable to scan dir ' + err);
        }
        files.forEach(function (file) {

            console.log(file);
        });
    });

这部分,单击按钮时,我可以在其中选择文件并将其上传到Azure blob存储. (正在工作)

And this part, where I can choose file and upload to Azure blob storage when click button. (it's work)

        document.getElementById('upload-button').addEventListener('click', () => {
        const file = document.getElementById('fileinput').files[0];
        blobService.createBlockBlobFromBrowserFile('mycontainer',
            file.name,
            file,
            (error, result) => {
                if (error) {
                    // Handle blob error
                } else {
                    console.log('Upload is successful');
                }
            });
    });

启动应用程序后,如何将这些代码合并到所有具有从C:ILJATEST自动上传的文件的文件夹中?

How to combine these codes, to all folders with files from C:ILJATEST uploaded automatically, when an app is started?

推荐答案

我看到您正在使用 Azure Storage JavaScript Client Library for Browsers ,以自动上传文件夹中的所有文件及其子文件夹.

I see you were using Azure Storage JavaScript Client Library for Browsers to automatically upload all files under a folder and its all sub-folders in brower.

这是我的示例代码,它们引用了官方示例 Azure Storage JavaScript Client Library Sample for Blob Operations 设置CORS规则并从Azure Storage Explorer获取sas令牌,它对我有用.

Here is my sample code which refer to the offical sample Azure Storage JavaScript Client Library Sample for Blob Operations to set CORS rules and get the sas token from Azure Storage Explorer, and it works for me.

<html>
<head>
<script src="https://dmrelease.blob.core.windows.net/azurestoragejssample/bundle/azure-storage.blob.js"></script>
</head>
<body>
<input type='file' id="files" name="files" multiple webkitdirectory directory />
<script>
// Render via server-side to pass the value of account name and sas token, even container name.
var accountName = '<your storage account>';
var SasToken = '<sas token like sv=2018-03-28&ss=bfqt&srt=sco&sp=rwdl&st=2019-04-11T06%3A48%3A24Z&se=2019-04-12T06%3A48%3A24Z&sig=xxxxxxxxxxxxxxxxxxxx'
var blobUri = 'https://' + accountName + '.blob.core.windows.net';
var blobService = AzureStorage.Blob.createBlobServiceWithSas(blobUri,SasToken);
var containerName = '<your container name>';
document.getElementById("files").addEventListener("change", function(event) {
    var files = event.target.files;
    console.log(files)
    for(var i in files) {
        blobService.createBlockBlobFromBrowserFile(containerName, files[i].webkitRelativePath, files[i], {blockSize : files[i].size}, function(error, result, response) {
            finishedOrError = true;
            if (error) {
                // Upload blob failed
            } else {
                // Upload successfully
            }
        })
    }
});
</script>
</body>
</html>

注意:

  1. 在这里,我使用file.webkitRelativePath(仅适用于Chrome或其他基于铬的浏览器,请参阅

  1. At here, I use file.webkitRelativePath (only works in Chrome or other browsers based on chromium, please refer to https://developer.mozilla.org/en-US/docs/Web/API/File/webkitRelativePath) instead of file.name, because it includes parent-folder path.

考虑到安全性,我建议尽可能减少sas令牌的到期时间,并在服务器端动态呈现帐户名和sas令牌的HTML页面.

Considering for security, I suggest to reduce the expire time for sas token as possible as you can and to render the HTML page for account name and sas token dynamically on server-side.

否则,您可以尝试将文件上传到服务器端的Azure存储.

Otherwise, you can try to upload files to Azure Storage on server-side.

首页index.html:

<form action='/upload' enctype="multipart/form-data" method="POST">
    <input type='file' id="files" name="files" multiple webkitdirectory directory />
    <input type="submit" value="Submit" />
</form>

使用expressmulter-azure-storage的服务器端:

var azure = require('azure-storage');
var accountName = '<your account name>';
var accountKey = '<your account key>';
const express = require('express');
const app = express();
const port = 3000;
var multer  = require('multer');
var MulterAzureStorage = require('multer-azure-storage');
var upload = multer({
    preservePath: true,
  storage: new MulterAzureStorage({
    azureStorageConnectionString: 'DefaultEndpointsProtocol=https;AccountName='+accountName+';AccountKey='+accountKey+';EndpointSuffix=core.windows.net',
    containerName: '<your container name>',
    containerSecurity: 'blob',
    fileName: function(file) {
        return file.originalname; // file.originalname includes parent-folder path name
    }
  })
});
app.use(express.static('public'));
app.post('/upload', upload.any(), function(req, res, next) {
    console.log(req.files);
    res.send('OK');
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

这篇关于如何自动将文件夹上载到azure?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆