Post to folder under a public S3 Bucket











up vote
0
down vote

favorite












I can successfully post a file object via AJAX in client javascript code from my browser to the ROOT of an s3bucket, using this code:



function upload_file(path, fileObj) {
var fd = new FormData();
fd.append('key', fileObj.name);
fd.append('acl', 'bucket-owner-full-control');
fd.append('Content-Type', fileObj.type);
fd.append("file",fileObj);

return $.ajax({
type : 'POST',
url : path,
data : fd,
processData: false, // Don't process the data
contentType: false, // Don't set contentType
success: function(json) { console.log('Upload complete!') },
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log('Upload error: ' + XMLHttpRequest.responseText);
}
});
}


The destination is a test-only temporary(!) globally public S3 bucket configured as follows:



public settings



acl



bucket policy



cors



My problem is that I cannot post to any folder below the root of this bucket. Concretely put, doing this...



upload_file('https://s3-us-west-2.amazonaws.com/bucket.example.com/', fileObj)


results in a successful upload to root bucket.



However doing this...



upload_file('https://s3-us-west-2.amazonaws.com/bucket.example.com/sounds/', fileObj)


returns this error from S3:



Upload error: 
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>MethodNotAllowed</Code>
<Message>The specified method is not allowed against this resource.</Message>
<Method>POST</Method>
<ResourceType>OBJECT</ResourceType>
<RequestId>DC6AA872FC4F96B4</RequestId>
<HostId>/AOtAuVcXnRZrQD7Rs+EmpZ2H5YDs5TPgEjmvMpVqSdZuPbnTtE/nh4p/Fgad8v00VQ93RKer8g=</HostId>
</Error>


Since only posting to the subfolder is the problem, I assume my code is correct and that I have misconfigured my folder. What am I missing? My intent after overcoming this problem is to proceed to a more traditional pre-signed URL approach, but still wanted to get the bucket permissions sorted first.










share|improve this question
























  • Are the subfolder permissions the same?
    – Nick Maroulis
    Nov 17 at 21:31










  • I cannot see how to permission folders directly, Nick, and have not permissioned the folder beyond simply creating it with defaults. But I did select the folder afterwards, and via Actions made it public, just for good measure. Does one have to recreate folders after changing the bucket permissions/cors/acls?
    – Mark Bordelon
    Nov 17 at 21:35












  • Just blew away the subfolder and recreated it with the defaults (and it states that the folder inherits the same settings from bucket). Still get the same error when I try to post to it. Made it public again. Same error.
    – Mark Bordelon
    Nov 17 at 21:43












  • Your CORS configuration is correct and the error message cited in the question isn’t related to CORS. If you were having a CORS problem, you’d be getting an error message from the browser on the client side. But instead you’re getting a server-side error message. So it seems the solution the problem needs to found somewhere other than in your CORS configuration.
    – sideshowbarker
    Nov 17 at 22:12










  • I am counting on that as well. But just in case, wanted to give that config so that others wouldn't ask for it. Thanks, sideshowbarker. Do you see any issues with the other configuration?
    – Mark Bordelon
    Nov 17 at 22:16

















up vote
0
down vote

favorite












I can successfully post a file object via AJAX in client javascript code from my browser to the ROOT of an s3bucket, using this code:



function upload_file(path, fileObj) {
var fd = new FormData();
fd.append('key', fileObj.name);
fd.append('acl', 'bucket-owner-full-control');
fd.append('Content-Type', fileObj.type);
fd.append("file",fileObj);

return $.ajax({
type : 'POST',
url : path,
data : fd,
processData: false, // Don't process the data
contentType: false, // Don't set contentType
success: function(json) { console.log('Upload complete!') },
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log('Upload error: ' + XMLHttpRequest.responseText);
}
});
}


The destination is a test-only temporary(!) globally public S3 bucket configured as follows:



public settings



acl



bucket policy



cors



My problem is that I cannot post to any folder below the root of this bucket. Concretely put, doing this...



upload_file('https://s3-us-west-2.amazonaws.com/bucket.example.com/', fileObj)


results in a successful upload to root bucket.



However doing this...



upload_file('https://s3-us-west-2.amazonaws.com/bucket.example.com/sounds/', fileObj)


returns this error from S3:



Upload error: 
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>MethodNotAllowed</Code>
<Message>The specified method is not allowed against this resource.</Message>
<Method>POST</Method>
<ResourceType>OBJECT</ResourceType>
<RequestId>DC6AA872FC4F96B4</RequestId>
<HostId>/AOtAuVcXnRZrQD7Rs+EmpZ2H5YDs5TPgEjmvMpVqSdZuPbnTtE/nh4p/Fgad8v00VQ93RKer8g=</HostId>
</Error>


Since only posting to the subfolder is the problem, I assume my code is correct and that I have misconfigured my folder. What am I missing? My intent after overcoming this problem is to proceed to a more traditional pre-signed URL approach, but still wanted to get the bucket permissions sorted first.










share|improve this question
























  • Are the subfolder permissions the same?
    – Nick Maroulis
    Nov 17 at 21:31










  • I cannot see how to permission folders directly, Nick, and have not permissioned the folder beyond simply creating it with defaults. But I did select the folder afterwards, and via Actions made it public, just for good measure. Does one have to recreate folders after changing the bucket permissions/cors/acls?
    – Mark Bordelon
    Nov 17 at 21:35












  • Just blew away the subfolder and recreated it with the defaults (and it states that the folder inherits the same settings from bucket). Still get the same error when I try to post to it. Made it public again. Same error.
    – Mark Bordelon
    Nov 17 at 21:43












  • Your CORS configuration is correct and the error message cited in the question isn’t related to CORS. If you were having a CORS problem, you’d be getting an error message from the browser on the client side. But instead you’re getting a server-side error message. So it seems the solution the problem needs to found somewhere other than in your CORS configuration.
    – sideshowbarker
    Nov 17 at 22:12










  • I am counting on that as well. But just in case, wanted to give that config so that others wouldn't ask for it. Thanks, sideshowbarker. Do you see any issues with the other configuration?
    – Mark Bordelon
    Nov 17 at 22:16















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I can successfully post a file object via AJAX in client javascript code from my browser to the ROOT of an s3bucket, using this code:



function upload_file(path, fileObj) {
var fd = new FormData();
fd.append('key', fileObj.name);
fd.append('acl', 'bucket-owner-full-control');
fd.append('Content-Type', fileObj.type);
fd.append("file",fileObj);

return $.ajax({
type : 'POST',
url : path,
data : fd,
processData: false, // Don't process the data
contentType: false, // Don't set contentType
success: function(json) { console.log('Upload complete!') },
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log('Upload error: ' + XMLHttpRequest.responseText);
}
});
}


The destination is a test-only temporary(!) globally public S3 bucket configured as follows:



public settings



acl



bucket policy



cors



My problem is that I cannot post to any folder below the root of this bucket. Concretely put, doing this...



upload_file('https://s3-us-west-2.amazonaws.com/bucket.example.com/', fileObj)


results in a successful upload to root bucket.



However doing this...



upload_file('https://s3-us-west-2.amazonaws.com/bucket.example.com/sounds/', fileObj)


returns this error from S3:



Upload error: 
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>MethodNotAllowed</Code>
<Message>The specified method is not allowed against this resource.</Message>
<Method>POST</Method>
<ResourceType>OBJECT</ResourceType>
<RequestId>DC6AA872FC4F96B4</RequestId>
<HostId>/AOtAuVcXnRZrQD7Rs+EmpZ2H5YDs5TPgEjmvMpVqSdZuPbnTtE/nh4p/Fgad8v00VQ93RKer8g=</HostId>
</Error>


Since only posting to the subfolder is the problem, I assume my code is correct and that I have misconfigured my folder. What am I missing? My intent after overcoming this problem is to proceed to a more traditional pre-signed URL approach, but still wanted to get the bucket permissions sorted first.










share|improve this question















I can successfully post a file object via AJAX in client javascript code from my browser to the ROOT of an s3bucket, using this code:



function upload_file(path, fileObj) {
var fd = new FormData();
fd.append('key', fileObj.name);
fd.append('acl', 'bucket-owner-full-control');
fd.append('Content-Type', fileObj.type);
fd.append("file",fileObj);

return $.ajax({
type : 'POST',
url : path,
data : fd,
processData: false, // Don't process the data
contentType: false, // Don't set contentType
success: function(json) { console.log('Upload complete!') },
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log('Upload error: ' + XMLHttpRequest.responseText);
}
});
}


The destination is a test-only temporary(!) globally public S3 bucket configured as follows:



public settings



acl



bucket policy



cors



My problem is that I cannot post to any folder below the root of this bucket. Concretely put, doing this...



upload_file('https://s3-us-west-2.amazonaws.com/bucket.example.com/', fileObj)


results in a successful upload to root bucket.



However doing this...



upload_file('https://s3-us-west-2.amazonaws.com/bucket.example.com/sounds/', fileObj)


returns this error from S3:



Upload error: 
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>MethodNotAllowed</Code>
<Message>The specified method is not allowed against this resource.</Message>
<Method>POST</Method>
<ResourceType>OBJECT</ResourceType>
<RequestId>DC6AA872FC4F96B4</RequestId>
<HostId>/AOtAuVcXnRZrQD7Rs+EmpZ2H5YDs5TPgEjmvMpVqSdZuPbnTtE/nh4p/Fgad8v00VQ93RKer8g=</HostId>
</Error>


Since only posting to the subfolder is the problem, I assume my code is correct and that I have misconfigured my folder. What am I missing? My intent after overcoming this problem is to proceed to a more traditional pre-signed URL approach, but still wanted to get the bucket permissions sorted first.







amazon-s3 permissions acl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 22:09









sideshowbarker

29.9k136988




29.9k136988










asked Nov 17 at 21:16









Mark Bordelon

3217




3217












  • Are the subfolder permissions the same?
    – Nick Maroulis
    Nov 17 at 21:31










  • I cannot see how to permission folders directly, Nick, and have not permissioned the folder beyond simply creating it with defaults. But I did select the folder afterwards, and via Actions made it public, just for good measure. Does one have to recreate folders after changing the bucket permissions/cors/acls?
    – Mark Bordelon
    Nov 17 at 21:35












  • Just blew away the subfolder and recreated it with the defaults (and it states that the folder inherits the same settings from bucket). Still get the same error when I try to post to it. Made it public again. Same error.
    – Mark Bordelon
    Nov 17 at 21:43












  • Your CORS configuration is correct and the error message cited in the question isn’t related to CORS. If you were having a CORS problem, you’d be getting an error message from the browser on the client side. But instead you’re getting a server-side error message. So it seems the solution the problem needs to found somewhere other than in your CORS configuration.
    – sideshowbarker
    Nov 17 at 22:12










  • I am counting on that as well. But just in case, wanted to give that config so that others wouldn't ask for it. Thanks, sideshowbarker. Do you see any issues with the other configuration?
    – Mark Bordelon
    Nov 17 at 22:16




















  • Are the subfolder permissions the same?
    – Nick Maroulis
    Nov 17 at 21:31










  • I cannot see how to permission folders directly, Nick, and have not permissioned the folder beyond simply creating it with defaults. But I did select the folder afterwards, and via Actions made it public, just for good measure. Does one have to recreate folders after changing the bucket permissions/cors/acls?
    – Mark Bordelon
    Nov 17 at 21:35












  • Just blew away the subfolder and recreated it with the defaults (and it states that the folder inherits the same settings from bucket). Still get the same error when I try to post to it. Made it public again. Same error.
    – Mark Bordelon
    Nov 17 at 21:43












  • Your CORS configuration is correct and the error message cited in the question isn’t related to CORS. If you were having a CORS problem, you’d be getting an error message from the browser on the client side. But instead you’re getting a server-side error message. So it seems the solution the problem needs to found somewhere other than in your CORS configuration.
    – sideshowbarker
    Nov 17 at 22:12










  • I am counting on that as well. But just in case, wanted to give that config so that others wouldn't ask for it. Thanks, sideshowbarker. Do you see any issues with the other configuration?
    – Mark Bordelon
    Nov 17 at 22:16


















Are the subfolder permissions the same?
– Nick Maroulis
Nov 17 at 21:31




Are the subfolder permissions the same?
– Nick Maroulis
Nov 17 at 21:31












I cannot see how to permission folders directly, Nick, and have not permissioned the folder beyond simply creating it with defaults. But I did select the folder afterwards, and via Actions made it public, just for good measure. Does one have to recreate folders after changing the bucket permissions/cors/acls?
– Mark Bordelon
Nov 17 at 21:35






I cannot see how to permission folders directly, Nick, and have not permissioned the folder beyond simply creating it with defaults. But I did select the folder afterwards, and via Actions made it public, just for good measure. Does one have to recreate folders after changing the bucket permissions/cors/acls?
– Mark Bordelon
Nov 17 at 21:35














Just blew away the subfolder and recreated it with the defaults (and it states that the folder inherits the same settings from bucket). Still get the same error when I try to post to it. Made it public again. Same error.
– Mark Bordelon
Nov 17 at 21:43






Just blew away the subfolder and recreated it with the defaults (and it states that the folder inherits the same settings from bucket). Still get the same error when I try to post to it. Made it public again. Same error.
– Mark Bordelon
Nov 17 at 21:43














Your CORS configuration is correct and the error message cited in the question isn’t related to CORS. If you were having a CORS problem, you’d be getting an error message from the browser on the client side. But instead you’re getting a server-side error message. So it seems the solution the problem needs to found somewhere other than in your CORS configuration.
– sideshowbarker
Nov 17 at 22:12




Your CORS configuration is correct and the error message cited in the question isn’t related to CORS. If you were having a CORS problem, you’d be getting an error message from the browser on the client side. But instead you’re getting a server-side error message. So it seems the solution the problem needs to found somewhere other than in your CORS configuration.
– sideshowbarker
Nov 17 at 22:12












I am counting on that as well. But just in case, wanted to give that config so that others wouldn't ask for it. Thanks, sideshowbarker. Do you see any issues with the other configuration?
– Mark Bordelon
Nov 17 at 22:16






I am counting on that as well. But just in case, wanted to give that config so that others wouldn't ask for it. Thanks, sideshowbarker. Do you see any issues with the other configuration?
– Mark Bordelon
Nov 17 at 22:16














1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










 fd.append('key', fileObj.name);


The object key is were you specify the full path to the file, without a leading slash. The only thing that needs to be different is this line:



fd.append('key', 'sounds/' + fileObj.name);





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355657%2fpost-to-folder-under-a-public-s3-bucket%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










     fd.append('key', fileObj.name);


    The object key is were you specify the full path to the file, without a leading slash. The only thing that needs to be different is this line:



    fd.append('key', 'sounds/' + fileObj.name);





    share|improve this answer

























      up vote
      2
      down vote



      accepted










       fd.append('key', fileObj.name);


      The object key is were you specify the full path to the file, without a leading slash. The only thing that needs to be different is this line:



      fd.append('key', 'sounds/' + fileObj.name);





      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






         fd.append('key', fileObj.name);


        The object key is were you specify the full path to the file, without a leading slash. The only thing that needs to be different is this line:



        fd.append('key', 'sounds/' + fileObj.name);





        share|improve this answer












         fd.append('key', fileObj.name);


        The object key is were you specify the full path to the file, without a leading slash. The only thing that needs to be different is this line:



        fd.append('key', 'sounds/' + fileObj.name);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 18 at 4:06









        Michael - sqlbot

        85.2k11125189




        85.2k11125189






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355657%2fpost-to-folder-under-a-public-s3-bucket%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Paul Cézanne

            UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

            Angular material date-picker (MatDatepicker) auto completes the date on focus out