Casting Azure Blob Item to Java File Object - java

So the existing code base where I work uses a regular Java File("a/directory/path") object for a massive amount of logic. Now my team wants me to use a file stored in the Azure Blob instead. I can get the file from the blob using the CloudBlobItem() java api. But this object is different than a regular java File() object. And I would have to change a bunch of stuff in the logic. Is there any blob item which can be casted to a regular File() object?

Short answer: No.
You're comparing two completely different things. Azure blobs are not files. You'd need to stream them down to where your code is running. Maybe to a file stream. Maybe write to disk. And then work with the file. You cannot just use an Azure blob like any other file I/O.
Note: If you're using Azure File Storage (which is an SMB share), then you do treat everything in that file store like you'd treat local storage. But it sounds like you're just using normal block blobs for your storage.

Related

Read and write Hashmap in Android

I have some data files that I use to initialize HashMaps in my Android program, and save the updated HashMaps during the program. These files are currently kept in the assets folder.
The only way to read and write a HashMap (that I am aware of) in regular java program is to use ObjectOutpuStream and ObjectInpuStream, however, it seems that currently these data files can only be accessed as InputStream object using getAssets().open(filepath), and I have no idea how to convert InputStream object to read and write HashMaps, any suggestion on this?
Is there any alternative ways to achieve this? (other than storing the data files in internal or external storage)

Securing database and saving all sorts of files

I am trying to save some documents/Images and files in sqlite database. The documents contain different types of pdf,.doc etc files and different format images. Now once I stored it in the blob format, I want to do a backup of the database to sdCard. I however want this backup to be readable only by app for securing the data inside.
Now the question is , for storing different types of files, Do I have to use some sort of other database and for securing. what should I do?
Best Regards
Android's built-in SQLite does not support encryption.
Try instead a library like SQLCipher.
One solution is to use blob and to store the raw data of the file as a column in the db as you are currently doing.
Other solution which I think is better to store the files on the Internal Storage and just store the file paths in the database. Internal storage is private for your application so no one will have access to the files, so your security concerns are taken care of.
see here for how to do this:
Creating folder in internal Memory to save files and retrieve them later
But be aware that can using too much internal storage can make people's phones run out of internal storage and they might decide to delete your app.

Execute a bytestream as a process in Java without writing to file

Basically, I want to be able execute a byte stream as a file, without writing said bytes to a file.
I want to do this in a windows environment.
You could say I want a to create a file, copy the stream to the file, and open the file as an executable, but I want the file to have no physical manifestation on the disk.
Thanks
This is not possible to do using the standard Java API. You will have to rely on some OS specific native implementations, either that do the whole thing for you, or that allow you to create some sort of RAM-disk on which you can place your temporary data and execute it.
Possibly related:
write file in memory with java.nio?
Supposing you bytestream is the one of a standard exe, and you don't want to call a whole virtualisation environnement, the short answer is no because internal adresses in the code wouldn't map.
Here's a more detailled answer :
process.start() embedded exe without extracting to file first c#

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.

Writing to text file in GAE

Has anyone figured out a way to write to text file on the server-side using Google App Engine (GAE). I understand the limitation of not being able to use FileWriter, but I was wondering if there was a work around. Thanks!
Not only can you not use FileWriter, you can not write to files at all, since you do not have access to the filesystem from within the GAE. It's impossible by design.
As a "work-around" (which I hesistate designating as such since this is a technically valid solution), you can emulate a filesystem using the GAE datastore using GAEVFS: http://code.google.com/p/gaevfs/
Try the blob store. Now, you can write to the blob store like you write files to the file system.
https://developers.google.com/appengine/docs/java/blobstore/overview#Writing_Files_to_the_Blobstore

Categories

Resources