How to Add folder in Minio bucket using c#? - minio

I am using Minio .net client library
my requirement is how can i store my files in folder structure like
ABC is Bucket, CMS is inner folder and CMS folder contains files so how can i achive this ?
amazon s3 doing same things using key value pair i.e ABC/CMS
EDIT:
How can i access files url in my .net project ?
Ex In ABC bucket i have abc.png file so how can i access image to display on HTML tag.
<img src="---any path---/abc.png">

Here is the solution of my own question.
var fileAsStreamData = file.OpenReadStream();
var fileName = "cms/" + file.FileName;
await _minioClient.PutObjectAsync("ASAP", objectName: fileName, data: fileAsStreamData, size: file.Length, contentType: file.ContentType);
Note:where "/" indicating folder structure in minio.
Ex. A/B/C/D/any_file_name so its consider as a inner folder like B is a inner folder of A and C is a inner folder of B and so on.

Creating an object whose name ends with a "/" will create a folder. It is an empty object that simulates a directory. link
I'd like to add that deleting the last object in a directory will also delete your folder. If you do not take that into account in your code, it might lead to bugs and malfunctioning.

Related

Download file based on user metadata fields from S3 bucket in java

I need to upload/download file in s3 bucket with user metadata organisationID, OrganisationType, fileType. Filename will be same(file.pdf, file.xls) but it contents will be differ based on organisationID, OrganisationType, fileType
if organisationID =1, OrganisationType=A, fileType = P then i need to download file.pdf
If organisationID =1, OrganisationType=A, fileType = X then i need to download file.xls.
I have below dbouts, How to have same keyname in bucket as filename is same in different scenarios
how to download file based on userMeta files without key
One way to do this is to mimic a hierarchical layout. You could make the key name for oraganizationID=1, OrganizationType=A fileType=P as 1/A/P/file.pdf. Similarly you could make the key name for organisationID=1, OrganisationType=A, fileType=X as 1/A/X/file.xls. Another way is to just embed the metadata in the key name without mimicking a directory structure like 1_A_P_file.pdf. Given the metadata and known file naming logic you can recreate the key name and retrieve the appropriate file. Strip away the metadata in the file name prior to returning the file to the end user.

google drive API search all children file with given folder ID

Here is how my folder is structured in google drive:
Picture
|----------Date1
|-----Pic1.png
|-----Pic2.png
|----------Date2
|-----Pic3.png
|-----Pic4.png
Right now I only have the ID of Picture folder (the parentID folder). Now I want to get Pic1 picture (inside Date1 folder). Is it possible to query only 1 time to get the picture with only the Picture folder's ID or I will have to query multiple times (get the Date1 and Date2 folder ID, then continue querying) ?
Here is my code right now
$imageName= "Pic1" // name of the image I want to find, which is Pic1
$folderId = "PictureFolderId" // Folder Picture's ID
$optParams = array(
'pageSize' => 1,
'fields' => 'nextPageToken, files(contentHints/thumbnail,fileExtension,id,name,size)',
'q' =>"mimeType contains 'image/' AND name contains '".$imageName."' AND '".$folderId."' in parents"
);
$results = $googleDriveService->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
print "No files found.\n";
} else {
// Do something.
}
If I understand you correctly, you want to:
Retrieve all the image files which are descendants (children, grandchildren, etc.) of a certain folder.
You want to retrieve this with a single API call.
Answer:
It is currently not possible to retrieve files in sub-folders with a single call. That's because a File resource (and a "folder" is a File) does not have information on what files are contained in this parent folder (let alone files contained in "sub-folders") and it only has information on the immediate parents of this folder, and not grandparents and so on, so to speak.
In order to retrieve all "descendants" of a certain folder, two main methods can be used (largely based on this answer by pinoyyid):
Method 1. List all folders:
List all folders in your Drive via Files: list, disregarding whether they have the main folder ID in parents. Let's call this list All folders.
Filter All folders, according to whether they have the main folder ID in parents. Let's call this list Direct children.
For each element in Direct children, filter All folders according to whether they have the corresponding direct child in parents. This way, you will retrieve a list of Grandchildren. Repeat the same process for each Grandchildren element, then for each Great-grandchildren and so on, until no folders are left. With this, you will have retrieved all the folders which are descendant of the main folder ID.
Once you have all the descendant folders, you have to make another Files: list to find the images which are children of any of those folders, or from the main folder. The search query could be something like this:
mimeType contains 'image/' AND ('mainFolderId' in parents OR 'descendant1ID' in parents OR 'descendant2ID' in parents OR...)
Note: This method requires less API calls and will be more appropriate if the file tree is relatively large.
Method 2. Iterate recursively:
Use Files: list to list all files in your main folder which are either folders (and so their own children should be listed) or images. In this case, the search query would be something like:
(mimeType = 'application/vnd.google-apps.folder' OR mimeType contains 'image/') AND 'folderId' in parents
For each child returned by this call, check if it's an image (MIME type containing image/) or a folder (MIME type: application/vnd.google-apps.folder). For each folder, list all files contained in it, using the method from previous step.
Repeat these two steps until there is no folder left. All the images you have retrieved are the "descendants" of your main folder.
Notes:
There is an open feature request in Issue Tracker to include ancestors in query searches, which could allow the retrieval of children in subfolders, and making this whole process much more easy. You could click the star on the top left of the referenced page in order to keep track of this request and to help prioritize it:
Issue #111763024. When will you allow us to use "ancestors" field in searches?
Reference:
How do I search sub-folders and sub-sub-folders in Google Drive?

How to get relative path of a file in a different library?

I have two projects in my solution:
Echipieri.Data (database repository) -> Locations.txt
Echipieri.Web (ASP.NET MVC)
I want to read the locations file from Data but I can't figure out the relative path.
I tryed:
string path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Locations.txt");
But it searches in Echipieri.Web not Echipieri.Data. How do I get it to search in Echipieri.Data folder?
Your path is relative to the application/site that is running the code. You need to move your text file to Echipieri.Web and supply the path to the file, or its contents, to your data layer.

Firefox Add-on: Get the item id of an existing bookmark folder by foldername

I'm trying to make my add-on save bookmarks in it's own bookmark folder.
This function created the desired folder, but I want it to return the id of the existing folder instead of forever making millions of new ones if it can find it by name:
function special_bookmark_folder()
{
menuFolder = bmsvc_service().bookmarksMenuFolder;
// <Try and find the bookmark folder here!>
return bmsvc_service().createFolder(menuFolder, ":: Bookmarkr ::", bmsvc_service().DEFAULT_INDEX);
// else return id of existing folder if it's found here
}
You have to use the Places query APIs, which isn't the most straightforward task. Thankfully there is an example with exactly what you want

drupal_execute populating image field

How to populate image field value with drupal_execute.
for ex my content type (test) has two additional fields
1. photo (image filed),
2. phid (text field)
for phid $form_state['values']['field_phid'][0]['value'] ='14'; . how to populate photo which is image field type
If the file is already uploaded to Drupal and has a file ID (fid) then you can just do
$form_state['values']['field_image_filed'][0]['fid'] = 17; //where 17 is the Drupal file ID of the file you want input
If the file isn't already uploaded it's a lot trickier. You'll first need to programmatically create the file. I can't walk you through it off-hand but a good place to look for a template as to how it should be done is the file_service_save() function in the Services module's file_service.inc:
http://drupalcode.org/viewvc/drupal/contributions/modules/services/services/file_service/file_service.inc?revision=1.1.2.7.2.3&view=markup&pathrev=DRUPAL-6--2-2
To be clear: I'm not saying you'll use file_service_save() to accomplish the upload, but that that code shows you what needs to be done. It will show you how to save the file to the server using file_save_data(), record the file to the Drupal "files" table, then call hook_file_insert to notify other modules that a file's been saved.
i found the solution as below . i dont know pros and cons but it works fine for me.
$image = "*******/test.jpg";
$field = content_fields('field_img', 'img_test');
$validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));
$files_path = filefield_widget_file_path($field);
$form_state['values']['field_img'][]= field_file_save_file($image, $validators, $files_path, FILE_EXISTS_REPLACE);

Resources