def download_asset(self, import_asset, redownload_asset): """ Download the URL specified for an Asset and save it to working storage """ if import_asset: item = import_asset.import_item.item download_url = import_asset.url asset = import_asset.asset elif redownload_asset: item = redownload_asset.item download_url = redownload_asset.download_url asset = redownload_asset else: logger.exception( "download_asset was called without an import asset or a redownload asset" ) raise asset_filename = os.path.join( item.project.campaign.slug, item.project.slug, item.item_id, "%d.jpg" % asset.sequence, ) try: # We'll download the remote file to a temporary file # and after that completes successfully will upload it # to the defined ASSET_STORAGE. with NamedTemporaryFile(mode="x+b") as temp_file: resp = requests.get(download_url, stream=True) resp.raise_for_status() for chunk in resp.iter_content(chunk_size=256 * 1024): temp_file.write(chunk) # Rewind the tempfile back to the first byte so we can temp_file.flush() temp_file.seek(0) ASSET_STORAGE.save(asset_filename, temp_file) except Exception: logger.exception("Unable to download %s to %s", download_url, asset_filename) raise
def download_asset(self, import_asset): """ Download the URL specified for an ImportItemAsset and save it to working storage """ item = import_asset.import_item.item asset_filename = os.path.join( item.project.campaign.slug, item.project.slug, item.item_id, "%d.jpg" % import_asset.sequence_number, ) try: # We'll download the remote file to a temporary file # and after that completes successfully will upload it # to the defined ASSET_STORAGE. with NamedTemporaryFile(mode="x+b") as temp_file: resp = requests.get(import_asset.url, stream=True) resp.raise_for_status() for chunk in resp.iter_content(chunk_size=256 * 1024): temp_file.write(chunk) # Rewind the tempfile back to the first byte so we can temp_file.flush() temp_file.seek(0) ASSET_STORAGE.save(asset_filename, temp_file) except Exception as exc: logger.error( "Unable to download %s to %s: %s", import_asset.url, asset_filename, exc, exc_info=True, ) raise