azure_storage_12.py 1.2 KB

12345678910111213141516171819202122232425262728
  1. def _save(self, name, content):
  2. """
  3. :param name:
  4. :param File content:
  5. :return:
  6. """
  7. original_name = name.get("original_name")
  8. blob_file_name = datetime.now().strftime("%Y%m%d-%H:%M:%S.%f_") + original_name
  9. # blob_name = "{}.{}".format(name.get("uuid"), original_name.partition(".")[-1])
  10. if hasattr(content.file, 'content_type'):
  11. content_type = content.file.content_type
  12. else:
  13. content_type = mimetypes.guess_type(original_name)
  14. if hasattr(content, 'chunks'):
  15. content_data = b''.join(chunk for chunk in content.chunks())
  16. else:
  17. content_data = content.read()
  18. print(f'Saving blob: container={self.azure_container}, blob={blob_file_name}')
  19. blob_client = self.connection.get_blob_client(container=self.azure_container, blob=blob_file_name)
  20. obj = blob_client.upload_blob(content_data)
  21. # create_blob_from_bytes(self.azure_container, name, content_data,
  22. #
  23. # content_settings=ContentSettings(content_type=content_type))
  24. af = AttachedFile(original_name, self.azure_container, blob_file_name)
  25. return af