0

I'm unable to get the object's metadata, always getting undefined as a result. This is the code snippet:

.then(function(answer) {
    if (answer === "publish") {
        //zip files here
        var zip = new JSZip();
        //var folder = zip.folder($scope.createYear + "/" + $scope.createMonth + "/" + $scope.JiraNo);
        var folder = zip.folder("ISE/" + $scope.JiraNo);
        //var imageFolder = zip.folder($scope.createYear + "/" + $scope.createMonth + "/" + $scope.JiraNo + "/img");
        var imageFolder = zip.folder("ISE/" + $scope.JiraNo + "/img");

        // Configure The S3 Object
        AWS.config.update({
            accessKeyId: $scope.creds.access_key,
            secretAccessKey: $scope.creds.secret_key
        });
        AWS.config.region = "ap-southeast-2";

        var bucket = new AWS.S3({
            params: {
                Bucket: $scope.creds.bucket,
                Prefix: $scope.creds.path + "/" + $scope.createYear + "/" + $scope.AddLeadingZeroToMonth($scope.createMonth) + "/" + $scope.JiraNo + "/img/"
            }
        });

        var listObjectPromise = bucket.listObjects(bucket.params).promise();
        listObjectPromise.then(function(data) {
            var promises = [];
            // download image files into an array of 64 bit encoded files
            for (var i = 0; i < data.Contents.length; i++) {
                var params = {
                    Bucket: $scope.creds.bucket,
                    Key: data.Contents[i].Key
                }
                var getObjectPromise = bucket.getObject(params).promise();
                promises.push(getObjectPromise);
                getObjectPromise.then(function(data) {
                    console.log("Loaded filename: ", data.Metadata);
                    imageFolder.file("image.png", data.Body);
                }).catch(function(error) {
                    console.log("Failed to retrieve an object: " + error);
                });
            }

            $q.all(promises).then(function(data) {
                console.log(">>>>>> All promises are resolved, zipping file >>>>>>");
                folder.file("campaign.json", JSON.stringify(JSONdata));

                if (CSSdata) {
                    UPLOADdataSize++;
                    CSSdata = CSSdata.replace(new RegExp("<div>", "g"), "");
                    CSSdata = CSSdata.replace(new RegExp("</div>", "g"), "");
                    CSSdata = CSSdata.replace(new RegExp("<br>", "g"), "");
                    CSSdata = CSSdata.replace(new RegExp("&nbsp;", "g"), "");
                    folder.file("style.scss", CSSdata);
                }

                if (JSdata) {
                    UPLOADdataSize++;
                    JSdata = JSdata.replace(new RegExp("<div>", "g"), "");
                    JSdata = JSdata.replace(new RegExp("</div>", "g"), "");
                    JSdata = JSdata.replace(new RegExp("<br>", "g"), "");
                    JSdata = JSdata.replace(new RegExp("&nbsp;", "g"), "");
                    folder.file("script.js", JSdata);
                }

                zip.generateAsync({
                        type: "blob"
                    })
                    .then(function(content) {
                        saveAs(content, $scope.JiraNo + ".zip");
                    });
            });
        }).catch(function(error) {
            console.log("S3 listing error: ", error);
        });
    }
}, function() {});

enter image description here

This is the response I get in the browser for Metadata object

Can someone please help how to retrieve a value from an object like that. Thank you.

RandomEli
  • 1,527
  • 5
  • 30
  • 53
  • When you say you are "getting undefined as a result", do you mean that data.Metadata.["ContentDisposition"] yields undefined? Why do you have the dot after data.Metadata? – jarmod Nov 17 '16 at 01:37
  • Sorry, typo. Dot is not supposed to be there. – Daniel Nashokin Nov 17 '16 at 02:06
  • Highly recommend copy/paste otherwise we have little confidence that the code you're showing us is the code that is actually running. Can you correct the code, and then tell us what you mean by "getting undefined as a result". As a result of what? – jarmod Nov 17 '16 at 02:10
  • You code is broken, can you reformat your code again? – RandomEli Nov 17 '16 at 02:15
  • Assuming that your issue is with data.Metadata["ContentDisposition"], then I'd recommend printing the data object (see http://stackoverflow.com/questions/957537/how-can-i-display-a-javascript-object). That will help you see what properties are available, and hopefully what type they are. – jarmod Nov 17 '16 at 02:19
  • To my understanding, you can use naive JSON.stringify(file) and pass the filename in the function. BTW, when you ask a question, your question should involve minimal amount of code, here 's a link of tutorial: http://stackoverflow.com/help/mcve – RandomEli Nov 17 '16 at 02:33
  • Seems like a bug in AWS SDK. Specifically setting the objects ContentDisposition and/or custom Metadata, still not getting any from S3. Tried deleting and re-uploading files, can see the actual metadata in S3 Bucket browser. Don't know....always getting empty Metadata object. – Daniel Nashokin Nov 17 '16 at 03:59
  • It's a pretty safe bet that the AWS SDK is not broken in such a fundamental way. Note that Content-Disposition is *not* part of `Metadata`. It's [`data.ContentDisposition`](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getObject-property). `Metadata` is only user-defined metadata, which is the `x-amz-meta-*` headers. – Michael - sqlbot Nov 17 '16 at 08:08

0 Answers0