-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to implement makeCopy of shared file #53
Comments
@SebastianMoyano just to clarify - you want to copy a file? (I'm not sure if Unfortunately, I don't see a It's possible to |
Yes, sorry for the late response, what I was looking for was to make a copy of a drive file which I have access to, this is because sometimes files that have been shared get a limit of downloads and the way to get around that is to make a copy into your own drive, at least for this specific situation it would help to have a feature like that. |
You should take a look to the code, comments and links in this issue of the original PyDrive project. I take the opportunity to ask if there is at least a simple documentation page which reflects improvements of this PyDrive2 project over the original PyDrive. It looks like PyDrive2 is not documented at all. |
@abubelinha it should be backward compatible to almost 100%, we tried to keep the same API. I would PyDrive2 for now is about making PyDrive stable and performant- a lot of bug fixes, we also fixed and improved tests and run it regularly on CI, we improved performance of things (e.g. download previously was reading the whole file into memory- this crazy and unacceptable for real cases). |
try:
file1 = drive.CreateFile({"id": source_file_id})
file1.FetchMetadata(fields="*")
if file1.get("mimetype") == MIME_SHORTCUT_TYPE:
file_id = file1.get("shortcutDetails").get("targetId")
file1 = drive.CreateFile({"id": file_id})
file1.FetchMetadata(fields="*")
file_id = file1.get("id")
if file1.get("mimeType") == MIME_FOLDER_TYPE:
text = "This is a folder and cannot be copied."
else:
body = {
"parents": [{"kind": "drive#fileLink",
"id": target_folder_id}],
'title': newtitle if newtitle else file1.get("title")
}
return drive.auth.service.files().copy(fileId=source_file_id,supportsAllDrives=True, body=body).execute()
except ApiRequestError as e:
if e.error.get("code") == 404:
text = (f"Source file {file_id} not exist.")
else:
text = e
except errors.HttpError as e:
if e.error.get("code") == 404:
text = (f"Source file {file_id} not exist.")
else:
text = e
except Exception as e:
text = e
print(text)
return None |
MIME_SHORTCUT_TYPE = 'application/vnd.google-apps.shortcut' def copy_file_remote(drive, source_file_id, target_folder_id, newtitle=None): |
Fixed by #188 |
Thanks a lot to @simone-viozzi for def Copy(self, target_folder=None, new_title=None, param=None):
"""Creates a copy of this file. Folders cannot be copied.
:param target_folder: Folder where the file will be copied.
:type target_folder: GoogleDriveFile, optional
:param new_title: Name of the new file.
:type new_title: str, optional
:param param: addition parameters to pass.
:type param: dict, optional
:raises ApiRequestError
:return: the copied file
:rtype: GoogleDriveFile
""" Where it says "folders cannot be copied", is that a limitation imposed by this function simple implementation? Anyway, to copy a whole folder I figure out the workaround would be creating my own function to create an empty target-copy folder, and then list the files inside the folder to be copied and make successive calls to this Copy function to copy them all. So, here are my questions: (1) Do I have to take care of hitting Drive limits (because of too many successive api calls), or does PyDrive2 somehow take care of this itself? If not ... how much time lapse do you suggest to I implement between Copy() calls? (2) Google Drive web interface permits to download a whole folder content as a zipped file (which contains all files and subfolders, so you can recreate them locally when unzipping). Should I open a new feature request issue, or would this already be possible using PyDrive2 too? (haven't found any examples or mentions to this when searching issues) (3) What about the opposite? (reconstruct a file structure in Gdrive, starting from a local zipped file) I guess it's not possible (this is the closest example I found in Google, but it uses Colab notebook, not a local Python terminal). My objective is to find the best PyDrive2 approach to copy structured contents (folders and files inside) from one Gdrive place to another. I am guessing the zip download (2) could be faster, but only if zip reupload (3) and unzip is also possible. Otherwise, option (1) is the only alternative I can figure out. Many thanks in advance for your suggestions. |
Thanks to @simone-viozzi :)
I think it is a limitation in the API + it's not even available in the UI.
PyDrive2 doesn't take care about this. But there is a simple wrapper that you could use: https://github.com/iterative/PyDrive2/blob/main/pydrive2/test/test_util.py#L46-L54
hmm, from trying to Google it a bit - that's the best I could find https://github.com/tanaikech/ZipFolder ... but I'm not sure there is a built-in API of that kind. I don't guarantee it, I would personally try to do a bit more research.
My feeling it that using the https://github.com/iterative/PyDrive2#fsspec-filesystem It handles retries also. But unfortunately, https://github.com/iterative/PyDrive2/blob/main/pydrive2/fs/spec.py#L536-L543 And it doesn't implement the |
Thanks a lot @shcheklein for all your comments. |
Yes you are correct. I plan on doing a PR to let the user chose if they want to do a server side copy (with |
thanks!
What would be the reason to support this? |
@shcheklein it's the current behavior of Lines 536 to 543 in 64a50a1
it's equivalent to download and re-upload. |
Yep, I know, but we don't need to preserve it - this should be an implementation detail. |
Not sure if my last question was misunderstood. So if inner files have to be copied one by one, current behaviour is indeed what I want for my use case (let Gdrive server make each file copy by itself, without downloading anything). I would only interested in downloading and reuploading if Gdrive api lets do it this way:
But I am afraid none of those functionalities are possible. I think it would be interesting to have such a function available as a PyDrive2 method. Hopefully, implementing some kind of inner control to avoid hitting Gdrive api usage limits (i.e. a parameter with a default time-lapse between Copy method calls).
Unfortunately I am totally git-illiterate so I cannot afford it (PRs and all that). |
Can I make For example, suppose I am trying to copy a file But can I use |
@elisevansbbfc I don't think it's possible, you would have to run a query, e.g.
|
I want to implement the makeCopy function that it can be used in google script, i tried doing this
But it didn't work, also how can I upload a specific file?
thanks in advance!
The text was updated successfully, but these errors were encountered: