Skip to content

Split-off download_file_from_url method from download_attachment #407

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions shotgun_api3/shotgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -2902,7 +2902,7 @@ def download_attachment(self, attachment=False, file_path=None, attachment_id=No
If an int value is passed in, the Attachment entity with the matching id will
be downloaded from the Shotgun server.
:param str file_path: Optional file path to write the data directly to local disk. This
avoids loading all of the data in memory and saves the file locally at the given path.
avoids loading all the data in memory and saves the file locally at the given path.
:param id attachment_id: (deprecated) Optional ``id`` of the Attachment entity in Shotgun to
download.

Expand All @@ -2924,16 +2924,37 @@ def download_attachment(self, attachment=False, file_path=None, attachment_id=No
"dict, int, NoneType value or"
"an int for parameter attachment_id"
)

url = self.get_attachment_download_url(attachment)
return self.download_file_from_url(url, file_path)

def download_file_from_url(self, url, file_path=None):
"""
Download the file associated with a Shotgun Attachment using the provided url.

>>> version = sg.find_one("Version", [["id", "is", 7115]], ["image"])
>>> url = version["image"]
>>> sg.download_file_from_url(url)

This method is used internally by :meth:`~shotgun_api3.Shotgun.download_attachment`.
This can be used to download other files from Shotgun/FPT that are not explicitly an attachment,
such as thumbnails.

:param str url: The url to download the file from. This should be a valid Shotgun URL
:param str file_path: Optional file path to write the data directly to local disk. This
avoids loading all the data in memory and saves the file locally at the given path.
:returns: If ``file_path`` is provided, returns the path to the file on disk. If
``file_path`` is ``None``, returns the actual data of the file, as str in Python 2 or
bytes in Python 3.
:rtype: str | bytes
"""
# write to disk
if file_path:
try:
fp = open(file_path, "wb")
except IOError as e:
raise IOError(
"Unable to write Attachment to disk using " "file_path. %s" % e
)
raise IOError("Unable to write Attachment to disk using " "file_path. %s" % e)

url = self.get_attachment_download_url(attachment)
if url is None:
return None

Expand Down
Loading