Create folder inside a bucket in minio using JAVA SDK - minio

The Minio JAVA SDK documentation doesn't clearly articulate how to create a folder inside a bucket.
Can somebody help me on this request ?
Thanks in Advance!

In the objectName parameter prefix of the putObject method, you can specify a folder name.
https://docs.minio.io/docs/java-client-api-reference#putObject
https://github.com/minio/minio/issues/2423#issuecomment-239408168
For example:
//objectName = folderName + "/" + fileName;
minioClient.putObject(bucketName, objectName, inputStream, contentType);

Create the bucket if its not present.
To add folder within it add it to the name of file.
Eg. File name: "sample.txt", Bucket Name: "main", SubFolder: "resrc"
filePath: file which you want to upload
minioClient.putObject("main", "resrc" + "/" + "sample.txt", filePath);

Create or check for exist.
// Create object ends with '/' (also called as folder or directory).
minioClient.putObject(
PutObjectArgs.builder().bucket("my-bucketname").object("path/to/").stream(
new ByteArrayInputStream(new byte[] {}), 0, -1).build());

Related

How to save a PDF file downloaded as byte[] array obtained from a Rest WS

I trying to save a PDF file, previously obtained from a REST WS as byte[] array.
byte[] response = await caller.DownloadFile(url);
string documentPath = FileSystem.CacheDirectory;
string fileName = "downloadfile.pdf";
string path = Path.Combine(documentPath, fileName);
File.WriteAllBytes(path, response);
My actual implementation don't shows any errors but when I looking for the file on cache folder, nothing are there, just a empty folder. Also try put the file in FileSystem.AppDataDirectory and Environment.GetFolderPath(Environment.SpecialFolder.Personal) but there are no files in any folder
What I'm missing?
Thank's in advance.
string documentPath = FileSystem.CacheDirectory;
string fileName = "downloadfile.pdf";
string path = Path.Combine(documentPath, fileName);
the path will like
"/data/user/0/packagename/cache/downloadfile.pdf"
As you save it to Internal Storage,you couldn't see the files without root permission,if you want to view it,you could use adb tool (application signed by Android Debug)
adb shell
run-as packagename
cd /data/data/packagename
cd cache
ls
then you could see the downloadfile.pdf
or you could save it to External storage,then you could find it in your device File Manager:
//"/storage/emulated/0/Android/data/packagename/files/downloadfile.pdf"
string path = Android.App.Application.Context.GetExternalFilesDir(null).ToString();

FileOutputStream throw FileNotFoundException when get File with Japanese in path

When using Google Drive API, I'm having this downloadMetadataFile() here to handle file:
public void downloadMetadataFile(String fileId, String folderStorePath, String fileName) throws IOException, GeneralSecurityException, GoogleException {
String path = folderStorePath + "/" + fileName
java.io.File file = new java.io.File(path);
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
Drive drive = createDrive();
drive.files().get(fileId)
.executeMediaAndDownloadTo(fileOutputStream);
}
}
When using above method with folder exists (izakayaTemplate + 居酒屋):
When path=/reports/template/izakayaTemplate/template3.png, the method working file and download template3.png successful from Google Drive
When path=/reports/template/居酒屋/template3.png, the method throw a FileNotFoundException at line try (FileOutputStream fileOutputStream = new FileOutputStream(file))
Can somebody please explain for me about this behavior?
Note:
I'm using SpringBoot 2.5, Java 8, Drive API v3
I'm running this project on Amazon linux 1 as a service by DaemonTool.
In the run config file, I have set
-Dfile.encoding=UTF-8
-Dsun.jnu.encoding=UTF-8 -Dfile.encoding=UTF-8 \
Update 1:
After debug for a while, I found out that the CanonicalPath is wrong for the new file I create but I don't know why it happen.
getPath: /reports/template/居酒屋/template3.png
getAbsolutePath: /reports/template/居酒屋/template3.png
getCanonicalPath: /reports/template/???/template3.png
After searching, I have found the solution for this problem:
Solution: Add export LANG=ja_JP.UTF-8 to the file run
Explanation: canonicalPath is the path file system considers the canonical means to reference the file system object to which it points. So in order for the system to get the canonicalPath to be correct, the environment must have set up correct language environment like in this document: https://docs.oracle.com/cd/E23824_01/html/E26033/glset.html. In my question, the correct language environment is ja_JP.UTF-8

JXA: Create a mailbox in Apple Mail

I am trying to create a sub-mailbox in Apple Mail using JavaScript.
I have the following code snippet (parent is a reference to the mailbox in which I want the new mailbox):
var mb = mail.Mailbox({name: "SubFolder"});
parent.mailboxes.push(mb);
The events log shows:
app = Application("Mail")
app.mailboxes.byName("Local").mailboxes.byName("Archive").mailboxes.push(app.Mailbox({"name":"SubFolder"}))
--> Error -10000: AppleEvent handler failed.
What am I doing wrong?
Thanks,
Craig.
Code now:
var mb = mail.Mailbox({name: "Local/Archive/Test Archive/SubFolder"})
logger.logDebug("mb = '" + Automation.getDisplayString(mb) + "'.");
mail.mailboxes.push(mb) // create the subfolder
This works as long as there are no spaces in the path.
I tried to force the space using \\ in front of it, but then you get "Test\ Archive" as the name.
So how do I get a space in the name to work?
Thanks.
To create a sub-folder, you need a name like a posix path --> "/theMasterMailbox/subMailBox1/subMailBox2/subMailBox3".
So, you need:
A loop to put the name of each parent folder into an array.
Use join('/') to join the elements of an array into a string.
Use mail.mailboxes.push(mb) instead of parent.mailboxes.push(mb)
Here's a sample script which creates a mailbox named "SubFolder" in the selected folder (the mailbox):
mail = Application('com.apple.Mail')
parent = mail.messageViewers()[0].selectedMailboxes()[0]
mboxNames = [parent.name()]
thisFolder = parent
try {
while (true) { // loop while exists the parent folder
mboxNames.unshift(thisFolder.container().name()) // add the name of the parent folder to the beginning of an array
thisFolder = thisFolder.container() // get the parent of thisFolder
}
} catch (e) {} // do nothing on error, because thisFolder is the top folder
mboxNames.push("SubFolder") // append the name of the new subFolder to the array
mBoxPath = mboxNames.join('/') // get a string (the names separated by "/")
mb = mail.Mailbox({name:mBoxPath})
mail.mailboxes.push(mb) // create the subfolder

Error in URL.getFile()

I am trying to open a file from URL.
Object of URL is created with getResource() method of ClassLoader.
Output URL returned from getResource() method is =
file:/C:/users/
After using URL.getFile() method which returns String as " /C:/users/ " it removes "file:" only not the "/ "
This / gives me a error in opening a file using new FileInputStream.
Error : FileNotFoundException
" / " in the starting of the filename causes the same problem in getting the path object.
Here , value of directory is retrieved from the URL.getResource().getFile()
Path Dest = Paths.get(Directory);
Error received is :
java.nio.file.InvalidPathException: Illegal char <:> at index 2: /C:/Users/
is anyone face such issue ?
Don't use URL.getFile(), it returns the "file" part of the URL, which is not the same as a file or path name of a file on disk. (It looks like it, but there are many ways in which there is a mismatch, as you have discovered.) Instead, call URL.toURI() and pass the resulting URI object to Paths.get()
That should work, as long as your URL points to a real file and not to a resource inside a jar file.
Example:
URL url = getClass().getResource("/some/resource/path");
Path dest = Paths.get(url.toURI());
The problem is that your result path contains leading /.
Try:
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Path path = Paths.get(loader.getResource(filename).toURI());

get complete address of a file

i need to get the full address of a file which needs to be uploaded by the user using browse button. i tried getAbsolutePath, getAbsoluteFile, getCanonicalPath but they all are giving tomcat/bin location. i need the full path of the file which is to be uploaded.
MultipartFile doc_file = studentInfoBean.getUploadedDocument();
String fileName = doc_file.getOriginalFilename();
String fileExtension = FilenameUtils.getExtension(fileName);
File file = new File(fileName);
File path = file.getAbsoluteFile();
//String path = path.toString()
thank you
You'll probably want to use MultipartFile.transferTo(File dest) to save the uploaded file locally. You can then do your conversion, and whatever you need to do with your .csv file (store it somewhere, send it back to the client, etc.) So the full code might be:
MultipartFile doc_file = studentInfoBean.getUploadedDocument();
File temp_file = new File(doc_file.getOriginalFilename());
doc_file.transferTo(temp_file);
//convert doc_file to .csv
//store locally permanently or return to client

Resources