Created
November 5, 2020 08:49
-
-
Save jbdesbas/3ad43fa81ab29a3716c7ce503162b1be to your computer and use it in GitHub Desktop.
Taxhub : upload media to S3 cloud
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""A lancer à la racine du dossier Taxhub, après avoir renseigner les accès du S3 | |
dans le fichier de config et activé l'environnement virtuel""" | |
import config | |
import os | |
import boto3 | |
import mimetypes | |
s3 = boto3.client('s3', | |
aws_access_key_id=config.S3_KEY, | |
aws_secret_access_key=config.S3_SECRET, | |
endpoint_url=config.S3_ENDPOINT, | |
region_name=config.S3_REGION_NAME) | |
for f in os.listdir(os.path.join( config.BASE_DIR,config.UPLOAD_FOLDER )): | |
print(f) | |
content_type = mimetypes.MimeTypes().guess_type( os.path.join( config.BASE_DIR,config.UPLOAD_FOLDER,f ) )[0] | |
if os.path.isfile( os.path.join( config.BASE_DIR,config.UPLOAD_FOLDER,f ) ): | |
with open( os.path.join( config.BASE_DIR,config.UPLOAD_FOLDER,f ),'r+b' ) as file : | |
s3.upload_fileobj(file, | |
config.S3_BUCKET_NAME, | |
os.path.join( config.S3_FOLDER, f ), | |
ExtraArgs={ | |
"ACL": "public-read", | |
"ContentType": content_type #sans ça le content type est par défaut binary/octet-stream | |
} ) | |
print("Les fichiers ont été téléchargés, pensez à mettre à jour les chemins dans la table taxonomie.t_media si besoin") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment