如何在java中使用Google Drive v3设置文件的应用属性?
引用说:“files.update with {'appProperties':{'key':'value'}}”,但我不明白如何将其应用于我的java代码。
我尝试过像
这样的事情file = service.files().create(body).setFields("id").execute();
Map<String, String> properties = new HashMap<>();
properties.put(DEVICE_ID_KEY, deviceId);
file.setAppProperties(properties);
service.files().update(file.getId(), file).setFields("appProperties").execute();
但后来我收到一条错误“资源正文包含不可直接写入的字段”。
获取数据:
File fileProperty = service.files().get(sFileId).setFields("appProperties").execute();
那么如何设置和获取文件的属性? 谢谢! :)
修改 我试过了
file = service.files().create(body).setFields("id").execute();
Map<String, String> properties = new HashMap<>();
properties.put(DEVICE_ID_KEY, deviceId);
file.setAppProperties(properties);
service.files().update(file.getId(), file).execute();
但我仍然收到相同的错误消息。
答案 0 :(得分:2)
避免在v3上出现此错误
"The resource body includes fields which are not directly writable."
调用update
时,您需要创建一个包含新更改的空File
并将其传递给update
函数。
我把这个和其他笔记写成v3 Migration Guide here。
答案 1 :(得分:0)
Drive API client for Java v3表示File.setAppProperties
需要Hashmap<String,String>
参数。尝试删除setFields("appProperties")
调用,因为您尝试覆盖appProperties
本身(此时您仍在调用Update)。
检索appProperties
时,您只需拨打getAppProperties
。
希望这有帮助!
答案 2 :(得分:0)
File fileMetadata = new File();
java.io.File filePath = new java.io.File(YOUR_LOCAL_FILE_PATH);
Map<String, String> map = new HashMap<String, String>();
map.put(YOUR_KEY, YOUR_VALUE); //can be filled with custom String
fileMetadata.setAppProperties(map);
FileContent mediaContent = new FileContent(YOUR_IMPORT_FORMAT, filePath);
File file = service.files().create(fileMetadata, mediaContent)
.setFields("id, appProperties").
.execute();
YOUR_IMPORT_FORMAT,在此链接https://developers.google.com/drive/api/v3/manage-uploads中填充值,示例代码下方有说明
setFields(“ id,appProperties”),用以下链接中的值填充:https://developers.google.com/drive/api/v3/migration,这是我认为最重要的部分,如果您未在setFields方法中设置该值,则需要附加输入将不会被写
答案 3 :(得分:0)
使用版本v3,可以为现有文件添加或更新appProperty,而不会出现此错误:
"The resource body includes fields which are not directly writable."
您应该这样做:
String fileId = "Your file id key here";
Map<String, String> appPropertiesMap = new HashMap<String, String>();
appPropertiesMap.put("MyKey", "MyValue");
appPropertiesMap.put("MySecondKey", "any value");
//set only the metadata you want to change
//do not set "id" !!! You will have "The resource body includes fields which are not directly writable." error
File fileMetadata = new File();
fileMetadata.setAppProperties(appPropertiesMap);
File updatedFileMetadata = driveService.files().update(fileId, fileMetadata).setFields("id, appProperties").execute();
System.out.printf("Hey, I see my appProperties :-) %s \n", updatedFileMetadata.toPrettyString());