Find file's previous parent folder in Google Drive [closed] - google-drive-api

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Let's say I have folder A and folder B. File.txt is stored in folder A and moved to folder B.
Given the file, is it possible to know the previous parent folder of the file?
It seems to be possible to know witch folder the file has been moved to, but not the origin folder.

You can use Google Drive Activity API which allows you to retrieve information about the changes made within a user's Google Drive.
Using activity.query()
Making Requests in the Drive Activity API
There are two ways to request activity: by Drive item, or for everything underneath a folder hierarchy.
itemName: The format for this key is "items/ITEM_ID". Typically this is a file in Drive. If you specify a folder for this key, it will show activity for the folder itself, such as when the folder was created or renamed.
ancestorName: The format for this key is "items/FOLDER_ID", and the response will include activity on all items in the subtree underneath this folder.
When no key is set, it defaults to using an ancestor name of "items/root", which will show activity for all items in your Google Drive.
Filtering
You can restrict the actions which might be returned in the DriveActivity object by constructing a filter string in the request.
To restrict by action type, use the field name detail.action_detail_case with the "has" operator (:) and either a singular value or a list of allowed action types enclosed in parentheses. Examples include:
detail.action_detail_case: RENAME
detail.action_detail_case:(CREATE UPLOAD)
-detail.action_detail_case:MOVE
These filtering conditions can be combined within a single filter string.
Sample Request Body:
{
"filter": "detail.action_detail_case:MOVE",
"itemName": "items/1kNGhKfVBtNHDNZPxUEzHYxxxxxx"
}
Filter all drive activity that has move ActionDetail
Primary criteria is to return activities for a specific file with file id: 1kNGhKfVBtNHDNZPxUEzHYxxxxxx
Response Body:
This will return a DriveActivity object that will contain a Move object under ActionDetail object. You can refer to the removedParents->driveItem to get the information regarding the previous parent folder of the file.
{
"activities": [
{
"primaryActionDetail": {
"move": {
"addedParents": [
{
"driveItem": {
"name": "items/1TrX6KcAJppWCj9GSUjSYn79Aqxxxx",
"title": "NewFolder",
.....
}
}
],
"removedParents": [
{
"driveItem": {
"name": "items/1YUrD6lUshY2IG0fIi0aFUoQRxxxx",
"title": "Untitled folder",
.....
}
}
]
}
},
......
],
"actions": [
{
......
}
],
"targets": [
{
"driveItem": {
"name": "items/1kNGhKfVBtNHDNZPxUEzHYxxxxxx",
"title": "sampledoc.json",
......
}
}
],
"timestamp": "2021-03-16T16:04:24.072Z"
}
]
}

Related

Aquiring a file list from a shared sub folder using the Google API list

Before writing java code I am trying to understand how the GoogleAPI works.
I am trying to get a list of files from a subdirectory of a shared drive,
If I use the file id from the high level shared folder I can get a list of all the files including subfolders.
Every time I try to get ONLY the files in a subfolder I get
{
"domain": "global",
"reason": "notFound",
"message": "Shared drive not found: 1S_4HV0vi3zPr-gt2lRTCp5hAJFooElCn",
"locationType": "parameter",
"location": "driveId"
}
I am using the website https://developers.google.com/drive/api/v3/reference/files/list
with the following parameters
Corpra = drive
deriveid
includeItemsFromAllDrives=true
pagesize=250
SupportsAllDrive=true
This is the partial output of the high level shared drive that I get the subfolder ID from
{
"kind": "drive#file",
"id": "1S_4HV0vi3zPr-gt2lRTCp5hAJFooElCn",
"name": "2021",
"mimeType": "application/vnd.google-apps.folder",
"teamDriveId": "0ACNyBdDB5wLXUk9PVA",
"driveId": "0ACNyBdDB5wLXUk9PVA"
},
What am I doing wrong ???
You cannot use the shared drive sub-directory folder id in Files.list().
This method requires a driveId which is the ID of the shared drive to search.
You can use Drives.list() to get a list of valid user shared drive
If you want to list the files inside the sub-directory folder, you need to add a query parameter that will indicate the file's parent folder id. See Search for files and folders
Note:
Some File Resource fields are not shown in the response body by default, if you want to show all fields, set fields parameter in Files.list() to *
For example:
My shared drive sub-folder id: '1V2bi9xN8aWDWzCvMWC4oCqmOGHh1xxxx'
If I want to list all files under this sub-folder, I need to include this query parameter: '1V2bi9xN8aWDWzCvMWC4oCqmOGHh1xxxx' in parents
Your request parameters will be:
Corpra = drive
deriveid
includeItemsFromAllDrives=true
pagesize=250
SupportsAllDrive=true
q = 'shared drive sub-folder id' in parents
Sample Response:
{
"kind": "drive#fileList",
"incompleteSearch": false,
"files": [
{
"kind": "drive#file",
"id": "1_XlY8tgcJgj13FItftGrF77ixxxxx",
"name": "subfolder_doc",
"mimeType": "application/vnd.google-apps.document",
"teamDriveId": "0AJWF4SKro9kyxxxx",
"driveId": "0AJWF4SKro9kyUxxxx"
}
]
}
(Update)
Here is a sample request in java using Drive.Files.List() methods
FileList result = service.files().list()
.setCorpora('drive')
.setDriveId('0ACNyBdDB5wLXUk9xxxx')
.setPageSize(250)
.setIncludeItemsFromAllDrives(true)
.setSupportsAllDrives(true)
.setQ("'1-mZjjMTrcmlNm6SnJQeSQxGxxxxx' in parents")
.execute();

VSCode json schema fileMatch pattern

In VSCode, I'm trying to add a sjson schema to a project I have. My .vscode/settings.json file is this:
{
"json.schemas": [
{
"fileMatch": [
"/*.json"
],
"url": "https://path/to/url.json"
}
]
}
```
This works, but it's currently applying to all .json files in all folders in the project, which I don't want. I want it to only apply to all .json files in the root directory. What do I have to put in the fileMatch property to enable this?
Update
Have tried the following. These work, but apply to every .json file:
"/*.json"
"*.json"
These don't work at all (no .json file gets the schema):
"${workspaceFolder}/*.json"
"${workspaceFolder}*.json"
"${workspaceFolder}\\*.json"
The above 3 without the {}
The above 4 with workspaceRoot
I ran into something similar, but instead of wanting to add everything in the root, I wanted to add all JSON files that are in a subfolder of the workspace (root) folder.
The solution I used was to add all JSON and then exclude the ones I didn't want (including the settings file itself):
{
"json.schemas": [
{
"fileMatch": [
"*.json",
"!/.vscode/settings.json",
"!/Some.json",
"!/SomeOther.json"
],
"url": "/Schema.json"
}
]
}
The ones that needed to be excluded were just a few, so I could just name them all.
A similar approach mightwork in the original problem, with something like this:
{
"fileMatch": [
"*.json",
"!/Folder1/*.json",
"!/Folder2/*.json"
],
}
In only problem here is that VS Code doesn't seem to allow wild cards for folder names. So using Glob like matching to exclude /*/*.json or /**/*.json doesn't help, unfortunately.

Google Storage JSON API missing folders item

i'm using Google Storage JSON API to get a json with all items inside my bucket to create a treelist inside my application.
My problem is that some folders items are missing inside this JSON.
This is the output of a folder item:
{
"kind": "storage#object",
"id": "id",
"selfLink": "selfLink",
"name": "pdf/",
"bucket": "bucket-name",
"generation": "1461762807657000",
"metageneration": "1",
"contentType": "application/x-www-form-urlencoded;charset=UTF-8",
"timeCreated": "2016-04-27T13:13:27.626Z",
"updated": "2016-04-27T13:13:27.626Z",
"storageClass": "STANDARD",
"timeStorageClassUpdated": "2016-04-27T13:13:27.626Z",
"size": "0",
"md5Hash": "",
"mediaLink": "link",
"crc32c": "",
"etag": ""
}
This is PDF folder, but for example Video folder missing despite items inside this folder are available in the JSON.
I obtain the same result trying API in this page Google Storage Object List
Google Cloud Storage has a flat namespace, so "Folders" don't actually exist. Instead we have a ListObject API with 'prefix' and 'delimiter' options, which allow for a very inefficient simulation of a hierarchy by setting 'prefix' to the "folder" you want to list, and setting the delimiter to '/'. This results in all of the objects in that folder being returned in the list call, and any "folders" being returned as common_prefixes.
Unfortunately what this means is that you can't actually 'create' a folder in google cloud storage. Folders only exist if there are objects in them, so when someone creates a "folder" in the UI, the UI creates a zero byte placeholder object in the folder with the name "", in order to make the "folder" exist.
If someone uploads a file with a name that places it into a folder without creating that folder via the UI first then this placeholder object will never be created.

GetResponse API v3 - Get contacts who open the newsletter

I'm having a hard time finding for the API in getting the contacts of the people who open the newsletter after sending it to a list of email. In the official API documentation of GetResponse, I didn't find a solution. Any idea or suggestion can help. thanks.
Though it's rather old now, I'll try to answer, maybe it helps someone.
Just as inside GetResponse web interface, you'll need to search contacts according to some criteria. These pages of the API docs describe how this is done:
http://apidocs.getresponse.com/v3/resources/search-contacts
Search contacts is the most complex part of the API. To save a search of contacts who opened a particular message you'll need to POST something like below to https://api.getresponse.com/v3/search-contacts/:
{
"name": "test_conditions", //can be any you like
"subscribersType": [
"subscribed" //can also be "undelivered", "removed" and "unconfirmed"
],
"sectionLogicOperator": "or", //or "and"
"section": [ //section can have up to 8 conditions; one saved search can have up to 4 sections
{
"campaignIdsList": [
"V" //you'll need to get campaigns' IDs with http://apidocs.getresponse.com/v3/resources/campaigns#campaigns.get.all
],
"logicOperator": "or",
"subscriberCycle": [
"receiving_autoresponder",
"not_receiving_autoresponder"
],
"subscriptionDate": "all_time", //"today", "yesterday", "this_month", "last_month", "this_week", "last_week" are also possible
"conditions": [
{
"conditionType": "opened",
"operatorType": "message_operator",
"operator": "autoresponder", //or "newsletter", or "split"
"value": "WTjXF" //message id, should be firstly got with a separate API call
}
]
}
]
}
More info on how the payload for such requests should be formed is here: http://apidocs.getresponse.com/v3/resources/search-contacts-reference
And the last point: if you don't need to save a search but only get the emails who've opened a message, in the object above you should remove the "name" property and post this to http://apidocs.getresponse.com/v3/search-contacts/contacts
More: http://apidocs.getresponse.com/v3/resources/search-contacts#search-contacts.contacts.form

Get file ID of a given path

is there a direct method to get file ID by giving a path (e.g. /some/folder/deep/inside/file.txt)? I know this can be done by recursively checking folder's contents, but a simple call would be much better.
Thanks
We currently don't have support for this, but the feedback will definitely be considered as we continue building out the v2 API.
An alternative to this would be to extract the target file/folder name from the path and search for it using the search API
like this: https://api.box.com/2.0/search?query=filename.txt
This gives back all the matching entries with their path_collections which provides the whole hierarchy for every entry. Something like this:
"path_collection": {
"total_count": 2,
"entries": [
{
"type": "folder",
"id": "0",
"sequence_id": null,
"etag": null,
"name": "All Files"
},
{
"type": "folder",
"id": "2988397987",
"sequence_id": "0",
"etag": "0",
"name": "dummy"
}
]
}
Path for this entry can be reverse engineered as /dummy/filename.txt
Just compare this path against the path you're looking for. If it matches, then that's the search result you're looking for. This is just to reduce the number of ReST calls you need to make to arrive at the result. Hope it makes sense.
Here is my approach on how to get a folder id based on a path, without recursively going through the whole tree, this can be easily adapted for file as well. This is based on PHP and CURL, but it's very easy to use it in any other application as well:
//WE SET THE SEARCH FOLDER:
$search_folder="XXXX/YYYYY/ZZZZZ/MMMMM/AAAAA/BBBBB";
//WE NEED THE LAST BIT SO WE CAN DO A SEARCH FOR IT
$folder_structure=array_reverse (explode("/",$search_folder));
// We run a CURL (I'm assuming all the authentication and all other CURL parameters are already set!) to search for the last bit, if you want to search for a file rather than a folder, amend the search query accordingly
curl_setopt($curl, CURLOPT_URL, "https://api.box.com/2.0/search?query=".urlencode($folder_structure[0])."&type=folder");
// Let's make a cine array out of that response
$json=json_decode(curl_exec($curl),true);
$i=0;
$notthis=true;
// We need to loop trough the result, till either we find a matching element, either we are at the end of the array
while ($notthis && $i<count($json['entries'])) {
$result_info=$json['entries'][$i];
//The path of each search result is kept in a multidimensional array, so we just rebuild that array, ignoring the first element (that is Always the ROOT)
if ($search_folder == implode("/",array_slice(array_column($result_info['path_collection']['entries'],'name'),1))."/".$folder_structure[0])
{
$notthis=false;
$folder_id=$result_info['id'];
}
else
{
$i++;
}
}
if ($notthis) {echo "Path not found....";} else {echo "Folder id: $folder_id";}