Change header on s3 file - amazon-s3

If I have a file on s3 how can I change metadata of that file?
It looks like I can "copy" it to the same location with new headers which would effectively be the same thing.
I'm using knox as the node client to do this. The file in question already has the Content-Type header set to video/mp4 but I want to change it to application/octet-stream. The reason for this is so that this link will trigger the browser to download the resource instead of displaying it in the browser window.
Link to knox source for this function
var filename = "/example/file.mp4",
headers = {'Content-Type': "application/octet-stream"};
client.copyFile(filename, filename, headers, function(error, resp) {
//response is successful
});
The response is successful, but when I reload the resource in s3 I don't see that headers have changed.
I can see that the underlying API call is this:
'PUT /example/file.mp4 HTTP/1.1\r\nContent-Type: application/octet-stream
x-amz-copy-source: /bucket/example/file.mp4
Content-Length: 0\r\nDate: Thu, 28 Jan 2016 21:13:12 GMT
Host: cc-video-archives-dev.s3.amazonaws.com
Authorization: <redacted>=\r\nConnection: close\r\n\r\n',

I was missing this header:
"x-amz-metadata-directive": "REPLACE"
var filename = "/example/file.mp4",
headers = {
"x-amz-metadata-directive": "REPLACE",
'Content-Type': "application/octet-stream"
};
client.copyFile(filename, filename, headers, function(error, resp) {
//response is successful
});

Related

Why is browser using cached response headers if they are not present in the actual response?

I realized that a response header would be present in the Network tab of the Chrome console even if that header wasn't set in express. I found this answer suggesting disallowing caching. What confuses me is why the cached response is still used even if a request is made to the server.
request from react
const baseURL = 'http://localhost:3001'
const axiosClient = axios.create({
baseURL,
withCredentials: true,
})
let accessToken
axiosClient.interceptors.response.use((response) => {
const { data, headers } = response
//store access token in memory
accessToken = headers['x-access-token']
console.log(accessToken)
// if (accessToken) axiosClient.defaults.headers['X-Access-Token'] = accessToken
return data
})
async me() {
return await axiosClient.get('/auth/me')
}
request reaches route
router.get('/me', (req, res) => {
// res.set('X-Access-Token', 'test 4')
res.send('me')
})
vscode debug console
res.getHeaders()
{x-powered-by: 'Express', access-control-allow-origin: 'http://localhost:3000', vary: 'Origin', access-control-allow-credentials: 'true', access-control-expose-headers: 'X-Access-Token'}
req.headers
{host: 'localhost:3001', connection: 'keep-alive', sec-ch-ua: '"Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"', accept: 'application/json, text/plain, */*', sec-ch-ua-mobile: '?0', …}
old token still appears in chrome
Since Chrome made a request to the server instead of just using the cached response (First, since there is no need to deliver the request to the origin server, then the closer the client and cache are, the faster the response will be), why isn't Chrome using the received response where the token header isn't present?

asp .net core server serve static file without specify any charset in the Content-Type Response Header

in my blazor server app (.NET6.0) i serve some static files and show them by embedding them a iframe (so the browser deal directly with the type of file, can be image, video, sound, pdf, etc)
I notice encoding problem on accentuated character on the txt and the html files when its shown in the iframe
I try to insert a inside the head on the iframe but same result
i notice on the http call to the file these Response Headers
accept-ranges: bytes
content-encoding: br
content-type: text/plain
date: Thu, 23 Dec 2021 13:15:47 GMT
etag: "1d7f7ff334b008b"
last-modified: Thu, 23 Dec 2021 13:15:48 GMT
server: Kestrel
vary: Accept-Encoding
Im surprise there is no utf-8 specified in the content-type header, im wondering if this is the source of my problem ?
I expected something like that content-type: text/plain; charset=utf-8
i try to play with the StaticFileOptions in Startup to change the headers but even put empty option maker the app broken at startup
app.UseStaticFiles(new StaticFileOptions {
});
//even doing this break the app, when the app start the file blazor.server.js finish in 404 on client side
So i can't really make something here
th serve my static files, i use a virtual directory on this manner
app.UseFileServer(new FileServerOptions
{
FileProvider = new PhysicalFileProvider(Sys.Web.AppliIs.Path_Webdav),
RequestPath = new PathString("/" + Sys.Web.AppliIs.WEBDAV_FOLDER),
EnableDirectoryBrowsing = false
});
I notice i don't have encoding problem when i open the link directly with chrome, its only inside my iframe for now, i can't explain that.
Thanks for your help
I was able to override the OnPrepareResponse to add the charset, i don't know why but i have to put ISO charset to resolve encoding problems
var lOptions = new FileServerOptions
{
FileProvider = new PhysicalFileProvider(Sys.Web.AppliIs.Path_Webdav),
RequestPath = new PathString("/" + Sys.Web.AppliIs.WEBDAV_FOLDER),
EnableDirectoryBrowsing = false,
};
lOptions.StaticFileOptions.OnPrepareResponse = (context) =>
{
var headers = context.Context.Response.Headers;
var contentType = headers["Content-Type"];
contentType += "; charset=ISO-8859-1";
headers["Content-Type"] = contentType;
};
app.UseFileServer(lOptions);
for me the subject is close, but if anybody know why i have to specify this charset im still intersted.

can't set header with fetch

I set a header for post a request with application/x-www-form-urlencoded form body;
here's my code
var headers = new Headers();
headers.append('Content-Type','application/x-www-form-urlencoded');
fetch(url, {
method: "POST",
mode:"cors",
header:headers,
body: qs.stringify(params)
})
But Content-type changed autoly in the request
Content-Type text/plain;charset=UTF-8
So that I can't receive this request params correctly.
Any one meet that?
CORS only allows a subset of content types. I lost lots of hours to this one. :)
See this answer. Even though application/x-www-form-urlencoded could be possible in a CORS request, I think it's very likely that your header is being overwritten by setting mode: 'cors' in your fetch call.

How can I change the filename in a fine-uploader blob request

Using the basic fine-uploader and addFile to insert a blob based entry.Everything goes ok until the server reject the request due to "security issues".
If I convert the blob to a file and send it to the fine-uploader the server is quite happy. The only difference between the two requests is the filename in the request header.
The file-uploader setName() doesn't appear to change to "blob" name.
fails:
Content-Disposition: form-data; name="qqfile"; filename="blob"
Content-Type: image/jpeg
Any way to replace the 'filename="blob"' with a name that contains the correct extension - blob.jpg, blob.gif,etc?
var myblob = new Blob([outputBuffer], {type: "image/jpeg"});
myblob.name = ofile;
var myfile = new File([myblob], 'ImageTest.jpg', {
lastModified: new Date(0), // optional - default = now
type: "image/jpeg" // optional - default = ''
});
fineUploader.addFiles({blob:myblob, name:ofile});
console.log(fineUploader.getName(0));
fineUploader.setName(0,ofile);
console.log(fineUploader.getName(0));
fineUploader.addFiles(myfile);

How To Set A Standalone Attachment Content Type

I've been able to create a standalone attachment, but the content_type ends up as multipart/form-data. What I am I doing wrong? The code is followed by the response from the post and then the request; you can see in the request that the content type is correct in the Request Payload.
Code:
function uploadFile() {
var fd = new FormData();
var file = document.getElementById('fileToUpload').files[0];
fd.append("fileToUpload", document.getElementById('fileToUpload').files[0]);
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("PUT", "http://usr:pswd#localhost:5984/db_test/testdoc7/"+ file.name +"?rev=1-967a00dff5e02add41819138abb3284d");
xhr.send(fd);
}
Response:
{
"_id": "testdoc7",
"_rev": "2-2841dcd640adb94de525e486be34052e",
"_attachments": {
"P9025287.JPG": {
"content_type": "multipart/form-data; boundary=----WebKitFormBoundary9QNXLDTeW13Gc1ip",
"revpos": 2,
"digest": "md5-VcoscthaPUYoWHBmCBaAnA==",
"length": 3083669,
"stub": true
}
}
}
Request:
Request URL:http://usr:pswd#localhost:5984/db_test/testdoc7/P9025287.JPG?rev=1-967a00dff5e02add41819138abb3284d
Request Method:PUT
Status Code:201 Created
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:3083669
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary9QNXLDTeW13Gc1ip
Cookie:AuthSession=YmRyaG9hOjUwOUFFNDg3OnR23NsQsqdQvnKp7HX_0g90grXw
Host:localhost:5984
Origin:http://localhost:5984
Referer:http://localhost:5984/estante_test/_design/library/html5/html5test.html
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
Query String Parametersview URL encoded
rev:1-967a00dff5e02add41819138abb3284d
Request Payload
------WebKitFormBoundary9QNXLDTeW13Gc1ip
Content-Disposition: form-data; name="fileToUpload"; filename="P9025287.JPG"
Content-Type: image/jpeg
------WebKitFormBoundary9QNXLDTeW13Gc1ip--
Response Headersview source
Cache-Control:must-revalidate
Content-Length:71
Content-Type:text/plain; charset=utf-8
Date:Wed, 07 Nov 2012 22:45:40 GMT
ETag:"2-2841dcd640adb94de525e486be34052e"
Location:http://localhost:5984/estante_test/testdoc7/P9025287.JPG
Server:CouchDB/1.2.0 (Erlang OTP/R15B)
Your code is PUT'ing the body of an entire form to CouchDB. I suspect if you look at the attachment, you'll find that not just the Content-Type is incorrect. Something like this should work better:
function uploadFile() {
var file = document.getElementById('fileToUpload').files[0];
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("PUT", "http://usr:pswd#localhost:5984/db_test/testdoc7/"+ file.name +"?rev=1-967a00dff5e02add41819138abb3284d");
xhr.send(file);
}
This works because the File interface inherits from Blob, so the XHR2 send algorithm will submit the file's raw data as well as setting the mime type based on the your file entry's .type attribute.
Note that at least some browsers do not provide a good MIME type guess for files, so you may still end up with "application/octet-stream" as the type unless you provide an override for yourself via xhr.setRequestHeader().