1234567891011121314151617181920212223242526272829303132333435363738 |
- def upload_files_to_s3_with_key(
- self, file_path, file_name, is_private, parent_doctype, parent_name
- ):
- """
- Uploads a new file to S3.
- Strips the file extension to set the content_type in metadata.
- """
- mime_type = magic.from_file(file_path, mime=True)
- key = self.key_generator(file_name, parent_doctype, parent_name)
- content_type = mime_type
- try:
- if is_private:
- self.S3_CLIENT.upload_file(
- file_path, self.BUCKET, key,
- ExtraArgs={
- "ContentType": content_type,
- "Metadata": {
- "ContentType": content_type,
- "file_name": file_name
- }
- }
- )
- else:
- self.S3_CLIENT.upload_file(
- file_path, self.BUCKET, key,
- ExtraArgs={
- "ContentType": content_type,
- "ACL": 'public-read',
- "Metadata": {
- "ContentType": content_type,
- }
- }
- )
- except boto3.exceptions.S3UploadFailedError:
- frappe.throw(frappe._("File Upload Failed. Please try again."))
- return key
|