python mino上传html文件设置为可在线查看

需求:通过python上传html到minio, 可在线查看

解决:更改桶权限,上传文件时指定content_type 和 metadata。

pip install minio

from minio import Minio
minioClient = Minio(
                    endpoint='minio.xxx.com',    # 文件服务地址
                    access_key='admin',          # 用户名
                    secret_key='admin',          # 密钥
                    secure=False)                # 设为True代表启用HTTPS


content_type='text/html'
metadata = {'Content-Type': 'text/html'}


    
示例:更改桶权限 设置公共可下载/查看
policy = '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:GetBucketLocation","s3:ListBucket"],"Resource":["arn:aws:s3:::%s"]},{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:GetObject"],"Resource":["arn:aws:s3:::%s/*"]}]}' % (bucket_name, bucket_name)

minioClient.set_bucket_policy(bucket_name=bucket_name, policy=policy)

# 上传html文件
minioClient.fput_object(bucket_name, object_name, file_path, content_type=content_type, metadata=metadata)

 

你可能感兴趣的:(python mino上传html文件设置为可在线查看)