0

I am having a list of 2 videos in a folder which I upload to AWS from my local directory when I call uploadDir function.

The videos are named dynamically to the path of their already existing file, because am re-uploading to replace the same file in the existing directory on aws

Example.

Video 1 name is: https://myBucketName.s3.amazonaws.com/Movies/video/comedy/93dcf22e84918d0efcb90ba2/Tom-41560712aa0f84f49ed130f2/Home Alone.mp4

Video 2 name is: https://myBucketName.s3.amazonaws.com/Movies/video/action/26wjf31g64918d0efcb90bw6/Ben-71560712aa0f84f49ed130f4/Mission Impossible.mp4

But because I can't save a filename with special characters I then replace the video name with special characters in my folder directory using the function below

var str1 = videoName.replace(':','X');
  var str2 = str1.replace(/[&\/\\#,<>{}]/g,'Q');

So I replaced all the ':' with 'X', and replaced all the '/' with 'Q'

So now the video name in my directory I want to upload back to AWS is looking like this

Replaced Video 1 name is: httpsXQQmyBucketName.s3.amazonaws.comQMoviesQvideoQcomedyQ93dcf22e84918d0efcb90ba2QTom-41560712aa0f84f49ed130f2QHome Alone.mp4

Replaced Video 2 name is: httpsXQQmyBucketName.s3.amazonaws.comQMoviesQvideoQactionQ26wjf31g64918d0efcb90bw6QBen-71560712aa0f84f49ed130f4QMission Impossible.mp4

So now I want to upload the videos back to their AWS path, which is originally the video name.

So for me to achieve this I believe I must first replace the original special characters, So all the 'X' will be ':', and all the 'Q' will be '/'

And the next step is for me to upload to the video name path.

How can I achieve this.

I already have a function uploading the videos to Aws bucket, but not to the directly I want the videos to be.

This is the code uploading the videos to Aws

var AWS = require('aws-sdk');
var path = require("path");
var fs = require('fs');


const uploadDir = function(s3Path, bucketName) {

    let s3 = new AWS.S3();

    function walkSync(currentDirPath, callback) {
        fs.readdirSync(currentDirPath).forEach(function (name) {
            var filePath = path.join(currentDirPath, name);
            var stat = fs.statSync(filePath);
            if (stat.isFile()) {
                callback(filePath, stat);
            } else if (stat.isDirectory()) {
                walkSync(filePath, callback);
            }
        });
    }

    walkSync(s3Path, function(filePath, stat) {
        let bucketPath = filePath.substring(s3Path.length+1);
        let params = {Bucket: bucketName, Key: bucketPath, Body: fs.readFileSync(filePath) };
        s3.putObject(params, function(err, data) {
            if (err) {
                console.log(err)
            } else {
                console.log('Successfully uploaded '+ bucketPath +' to ' + bucketName);
            }
        });

    });
};

uploadDir(path.resolve('path-containing-videos'), 'bucket-name');
Otis
  • 339
  • 2
  • 16
  • 1
    What would happen if you tried to handle [Queen X](https://www.imdb.com/title/tt0008485/)? Not to mention, these two characters are not the only special characters. You'll probably be better off if you base64 encode the URL and use that as the filename, or use a random ID and store the URL out of band, like in a database. – Anon Coward Jan 02 '23 at 18:01
  • Why don't you URL encode the file name? – Brian Jan 02 '23 at 18:03
  • FYI the bucket name and object key uniquely identify the S3 object, so there's no need for you to include `https://` or `s3.amazonaws.com` in your encoded local filename. Also, no need to use `var` in JavaScript any more. – jarmod Jan 02 '23 at 18:11
  • Be aware of [streaming options](https://stackoverflow.com/questions/37336050/pipe-a-stream-to-s3-upload) to avoid synchronously reading very large files. – jarmod Jan 02 '23 at 18:22
  • @Anon Coward Thanks, I encoded the URL – Otis Jan 02 '23 at 19:18
  • @jarmod Thank you, I just learned that object key uniquely identify the S3 object. – Otis Jan 02 '23 at 19:19
  • @jarmod, What if I want to delete the videos on my local folder after uploading, how will I handle that? – Otis Jan 02 '23 at 19:21
  • Just delete the relevant file when handling s3.putObject success for that file. You know the local filename/path because you needed it to read the file contents (or stream the content). – jarmod Jan 02 '23 at 19:25

0 Answers0