简体   繁体   中英

Cannot set the file modified date when inserting file to Google Drive

I am using the Google Drive API Java client library to insert files to Google Drive. Any file that is uploaded to Drive takes on the modified time of when it is uploaded, not the original file modified time. I've used the setModifiedDate of File object to set the date.

On seeing the documentation at Google Developers , I see the following text -

Last time this file was modified by anyone (formatted RFC 3339 timestamp). This is only mutable on update when the setModifiedDate parameter is set.

However, I cannot find the parameter setModifiedDate in the documentation, and neither the Java library has the method setSetModifiedDate(Boolean arg) in the class com.google.api.services.drive.Drive.Files.Insert (On the contrary, the update class com.google.api.services.drive.Drive.Files.Update contains the setSetModifiedDate(Boolean arg) method which works perfectly).

This code works for me to keep the modified date & time of a file before I upload it to Drive.

long modifiedDateTime = new java.io.File("/path/to/file").lastModified();
Time t = new Time();
t.set(modifiedDateTime);
t.switchTimezone(Time.getCurrentTimezone());
String modifiedDateAsRfc3339 = t.format3339(false);

// create the file to upload here
com.google.api.services.drive.model.File body = new File();
body.setTitle("/path/to/file");
body.setMimeType("mimetype");
// here is where you set the modified date to match the file's modified date
body.setModifiedDate(com.google.api.client.util.DateTime.parseRfc3339(modifiedDateAsRfc3339));


InputStreamContent mediaContent;
......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM