Downloading a file to modify it on Amazon S3 - java

So I am using S3 to store basically audio files, I stream those files with cloudfront. I need to modify the metadata of those files (not the metadata of the s3 object, but the tags of the music), or convert those files to another format (mp3 to m4a, etc). So the way I see it I need to download these files to my server, modify the files or transcode this files, and reupload the files.
I see some ways to do this but I have some doubts on which is the correct way or best way to do it.
So one way would be to download the file with the following code
S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
System.out.println("Content-Type: " + object.getObjectMetadata());
//displayTextInputStream(object.getObjectContent());
And I could use File to write the file to my server.
My question heres is how do I obtain just the file name from the S3Object, I was looking in the metadata and tried to use getContentDisposition(), but it returns null, looking directly in AWS Console I see the proper name of the file without the path.
The other idea I have is to use cloudfront to download the file, creating a download distribution.
Can I work directly with and inputstream to modify the metadata?

You can save this file under any name you choose, just like any other file you download through HTTP.
However, as you mentioned in your question:
String disposition = object.getObjectMetadata().getContentDisposition()
Should give you the optional Content-Disposition HTTP header, which specifies presentation information for the object such as the recommended file-name for the object to be saved as.
This function returns null if the Content-Disposition header hasn't been set.
This is from Amazon online documentation: For more information on how the Content-Disposition header affects HTTP client behavior, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.5.1.

Related

How to change the name of file uploaded to GCS using Bloblstore API

I'm using JAVA Blobstore API to upload files directly to the GCS. I have followed this
https://cloud.google.com/appengine/docs/java/blobstore/#Java_Using_the_Blobstore_API_with_Google_Cloud_Storage
This is working seamlessly. But when I tried to view or download the file, from GCS, its actually showing a different name than I uploaded. Its happening while uploading itself. I think, It takes a random blobkey as file name.
Is there any way to change the filename in GCS after uploading programatically, or any way to upload with custom name using Blobstore API.
Any help would be greatly appreciated.
Thanks.
It's not possible to set the name for files uploaded to GCS via Blobstore API. Direct object renaming is not possible on GCS. As a workaround you can:
Get object name from returned FileInfo.
Copy object to a new object of desired name.
Delete old object.

I want to read a file which is on hdfs

I have tried by giving path as" hdfs://localhost:9000/path to file" but still its not working is there any other way to read file?
I want to give the file path in program,but not as an argument on terminal...
If you have WebHDFS enabled (dfs.webhdfs.enabled set to true in hdfs-site.xml) then you can access the file with a simple REST call. Many client libraries can open HTTP URI's directly.
For Java, follow the example in Open stream from uri and construct the URI for your file using the examples in the WebHDFS REST API documentation. For example, I was able to open a file from HDFS on my cluster using the following URI: "http://namenode:50070/webhdfs/v1/sampledata/sample.log?op=OPEN"
If what you want is how read the contents of that file using Java code, then have a look at my answer: Programatically reading contents of text file stored in HDFS using Java.

How to make client download xml file from server?

My server is implemented with few servlets when each one is responsible for different task.
I need to make client to download a specified xml file from server when a SAVE button in html page pressed.
I've read that the best way is to host file on server and just let client download, but I don't know how to implement it.
Any example will be highly appreciated. :)
p.s.
I'm using JAVA.
Do these steps:
Set proper MIME-type for your file. If you do not want (there's no suitable MIME-type) to set MIME-type specific to your file type, then set it to application/octet-stream
Set content length to the response
Set content disposition
Open output binary stream, then read your file and write its contents to this output stream.
That's it.
Here is the sample code

Validation to check whether its a ".txt" file

I have this particular piece of code for restricting the users to upload image files only.
if (!fileName.getContentType().startsWith("image/"))
errors.add("", new ActionError("errors.imageFile.contentType"));
Similary I want the users to upload only files with extension ".txt" in another scenario. What MIME type should I use or please let me know the code which will be helpful for achieving this task.
Typically the mime type for text files is text/plain
Text files have the following MIME type:
text/plain
However, according to this site, it is not the only one. You can use Apache's FileNameUtils getExtension method to get the extension of the file.
What MIME type should I use ..?
Content-Type: text/plain
I want the users to upload only files with extension ".txt" in another scenario.
The mime type for plain text files is "text/plain". Or you can check the name of the uploaded file.
However, these won't prevent users uploading non-text files. All they need to do (on Windows) is to rename a non-text file to have the ".txt" extension ... and then upload it.
If you really want to make sure that users only upload text, you need to test the files after they have been uploaded.

How to store only image path (URL) in database rather than image itself?

I've googled for some efficient image storage solutions and got very exciting line to read that you should store only image path rather that whole image in the database.
Now I am working on a Java web project based on MVC and is willing too know extra about this topic. Specifically I want to know if I am able to save my image directly to any image hosting website from my Servlet which instantly provide me a link that I will store in my database column for future use?
you should store only image path rather that whole image in the database.
That's indeed recommended. Storing binary data in a database makes semantically no utter sense. You cannot index it, nor search in it, etcetera. It's just "dead" data. You can store it as good directly on the disk file system and then store its unique identifier (usually just the filename) in the database. The filename can be a varchar which is indexable (which thus allows for faster SELECT ... WHERE).
I want to know if I am able to save my image directly to any image hosting website from my Servlet which instantly provide me a link that I will store in my database column for future use?
I'm not sure what's your concrete problem here. You should realize that transferring bytes is after all just a matter of reading an arbitrary InputStream and writing it to an arbitratry OutputStream. Your concrete question should rather be, "How do I get an InputStream of the uploaded image?", or "How do I get an OutputStream to the local disk file system?", or maybe "How do I get an OutputStream to the image hosting website?".
Getting the uploaded image's InputStream is easy. All decent file upload APIs offer kind of a getInputStream() method. See also How to upload files to server using JSP/Servlet? Getting an OutputStream to a File on the local disk file system is also easy. Just construct a FileOutputStream around it.
File file = File.createTempFile(prefix, suffix, "/path/to/uploads");
InputStream input = uploadedFile.getInputStream();
OutputStream output = new FileOutputStream(file);
// Now write input to output.
String uniqueFileName = file.getName();
// Now store filename in DB.
Getting an OutputStream to some other host is a story apart. How do you want to connect to it? Using FTP? Use FTPClient#appendFileStream(). Or using HTTP (eek)? Use URLConnection#getOutputStream() or HttpClient. You should ask a more finer grained question about that if you stucks.
Finally, in order to get this image by URL (by either <img src> or direct request or whatever), read this answer: Reliable data serving.
Here's a tutorial how to upload a file to a server using servlets/JSP. Then use Apache Commons IO to save it to some directory on the server. Save file's path in the database and use it next time this file is asked for.
Sure, you can use apache commons api to upload your image to specified folder in server, and you can change its name as you wish, what ever it may be the image format you can save the path in Database and upload the image using servlet. apache commons api is for free, you will get the proper documentation apache site.

Categories

Resources