Google 驱动器 API / 将文件 属性 存储为数字而不是字符串
Google Drive API / Storing a file property as a number instead of string
鉴于 Google 驱动器 API 的 files
reference page,似乎可以使用 'properties' 添加的元数据只能是字符串。
{
"kind": "drive#file",
"id": string,
"name": string,
"mimeType": string,
"description": string,
"trashingUser": { },
"trashedTime": datetime,
"parents": [string],
"properties": { (key): string },
}
但是我需要存储一个时间戳,它基本上是一个整数。
当 querying 时,我希望能够 select 时间戳小于或大于给定值的所有文件。
请问有什么办法可以实现吗?
如果这个新的 属性 'timestamp' 是一个字符串,如何使用 >
或 <
运算符?
非常感谢您的帮助。
祝你有美好的一天,
最佳,
PS: 我正在使用 Google Drive API python 客户端。
我相信你的情况和目标如下。
- 在您的情况下,在您的 Google 驱动器中有
properties
以 timestamp
作为键和数字值的文件。
- 您想通过使用一个值与
timestamp
的值进行比较来检索文件。
- 从
a timestamp, which is basically an int
开始,timestamp
的值为unix时间。
- 您想使用 Python 的 googleapis 实现此目的。
为了达到你的目的,我提出以下答案。
问题和解决方法:
现阶段,为了在Drive API中使用files.list方法的搜索查询的properties
来搜索文件,需要使用[=17] =].在这种情况下,该值必须是字符串类型。而且,不幸的是,例如properties
中带有timestamp
的文件不能被properties has {key='timestamp'}
直接检索。这似乎是当前的规范。所以很遗憾,在当前阶段,您要检索的文件无法使用搜索查询直接检索。
所以在这个回答中,我想提出一个解决方法。此解决方法的流程如下。
- 使用驱动器 API 中的 files.list 方法检索 Google 驱动器中的所有文件。
- 检索
properties
中以 timestamp
作为关键字的文件。此时,timestamp
的值与输入的值进行比较。
当根据上述变通方法准备示例脚本时,它变成如下。
示例脚本:
compareValue = datetime.datetime(2020, 7, 12).timestamp() # Please input the date value you want to compare.
service = build('drive', 'v3', credentials=creds)
fileList = []
pt = ""
while True:
res = service.files().list(pageSize=1000, pageToken=pt if pt != "" else "", fields="nextPageToken, files(id, name, properties)").execute()
files = res.get('files', [])
filesWithProp = [f for f in files if f.get('properties') and f.get('properties').get('timestamp') and int(f.get('properties').get('timestamp')) > compareValue]
fileList.extend(filesWithProp)
pt = res.get('nextPageToken')
if not pt:
break
print(fileList)
- 在当前脚本中,可以检索比
compareValue
更新的文件。
- 当您从
int(f.get('properties').get('timestamp')) > compareValue
修改为int(f.get('properties').get('timestamp')) < compareValue
时,可以检索比compareValue
更早的文件。
注:
- 在这个示例脚本中,
properties
的键和值分别是timestamp
和unix时间。所以请注意这一点。
- 此示例脚本假定您已经能够使用 Drive API 和 googleapis for Python.
获取文件元数据
- 如果要检索的文件放在特定的文件夹中,您也可以使用
'folderId' in parents
这样的查询参数。这样,可以降低工艺成本。
参考文献:
鉴于 Google 驱动器 API 的 files
reference page,似乎可以使用 'properties' 添加的元数据只能是字符串。
{
"kind": "drive#file",
"id": string,
"name": string,
"mimeType": string,
"description": string,
"trashingUser": { },
"trashedTime": datetime,
"parents": [string],
"properties": { (key): string },
}
但是我需要存储一个时间戳,它基本上是一个整数。
当 querying 时,我希望能够 select 时间戳小于或大于给定值的所有文件。
请问有什么办法可以实现吗?
如果这个新的 属性 'timestamp' 是一个字符串,如何使用 >
或 <
运算符?
非常感谢您的帮助。 祝你有美好的一天, 最佳,
PS: 我正在使用 Google Drive API python 客户端。
我相信你的情况和目标如下。
- 在您的情况下,在您的 Google 驱动器中有
properties
以timestamp
作为键和数字值的文件。 - 您想通过使用一个值与
timestamp
的值进行比较来检索文件。 - 从
a timestamp, which is basically an int
开始,timestamp
的值为unix时间。 - 您想使用 Python 的 googleapis 实现此目的。
为了达到你的目的,我提出以下答案。
问题和解决方法:
现阶段,为了在Drive API中使用files.list方法的搜索查询的properties
来搜索文件,需要使用[=17] =].在这种情况下,该值必须是字符串类型。而且,不幸的是,例如properties
中带有timestamp
的文件不能被properties has {key='timestamp'}
直接检索。这似乎是当前的规范。所以很遗憾,在当前阶段,您要检索的文件无法使用搜索查询直接检索。
所以在这个回答中,我想提出一个解决方法。此解决方法的流程如下。
- 使用驱动器 API 中的 files.list 方法检索 Google 驱动器中的所有文件。
- 检索
properties
中以timestamp
作为关键字的文件。此时,timestamp
的值与输入的值进行比较。
当根据上述变通方法准备示例脚本时,它变成如下。
示例脚本:
compareValue = datetime.datetime(2020, 7, 12).timestamp() # Please input the date value you want to compare.
service = build('drive', 'v3', credentials=creds)
fileList = []
pt = ""
while True:
res = service.files().list(pageSize=1000, pageToken=pt if pt != "" else "", fields="nextPageToken, files(id, name, properties)").execute()
files = res.get('files', [])
filesWithProp = [f for f in files if f.get('properties') and f.get('properties').get('timestamp') and int(f.get('properties').get('timestamp')) > compareValue]
fileList.extend(filesWithProp)
pt = res.get('nextPageToken')
if not pt:
break
print(fileList)
- 在当前脚本中,可以检索比
compareValue
更新的文件。 - 当您从
int(f.get('properties').get('timestamp')) > compareValue
修改为int(f.get('properties').get('timestamp')) < compareValue
时,可以检索比compareValue
更早的文件。
注:
- 在这个示例脚本中,
properties
的键和值分别是timestamp
和unix时间。所以请注意这一点。 - 此示例脚本假定您已经能够使用 Drive API 和 googleapis for Python. 获取文件元数据
- 如果要检索的文件放在特定的文件夹中,您也可以使用
'folderId' in parents
这样的查询参数。这样,可以降低工艺成本。