You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3 Boto3 AWS S3:上传后获取永久公开HTTPS对象URL

Hey there! Let's figure out how to get that permanent public HTTPS URL right after uploading your file—no pre-signed URLs needed. The exact steps depend on which cloud storage service you're using, so I'll walk you through the most common scenarios with code examples to make it straightforward.

AWS S3 Setup

First, you need to ensure your bucket or target object has public read permissions. You can set a bucket-wide policy for public access (if that fits your use case) or explicitly set the object's ACL to public-read during upload.

Code Example (Python with Boto3)

import boto3
import urllib.parse

s3 = boto3.client('s3')
bucket_name = "your-bucket-name"
local_file_path = "path/to/your/local/file.jpg"
object_key = "folder/your-file-name.jpg"  # Path within the bucket

# Upload file and set public-read ACL
s3.upload_file(
    local_file_path,
    bucket_name,
    object_key,
    ExtraArgs={'ACL': 'public-read'}
)

# Encode object key to handle special characters (spaces, non-ASCII, etc.)
encoded_key = urllib.parse.quote(object_key)
# Construct the public HTTPS URL (adjust region if needed, e.g., s3.us-west-2.amazonaws.com)
public_url = f"https://{bucket_name}.s3.amazonaws.com/{encoded_key}"

# Now you can store this public_url directly in your database
print(public_url)

Note: If you've enabled static website hosting for your bucket, you can also use the static site URL format: https://{bucket-name}.s3-website-{region}.amazonaws.com/{encoded_key}

Google Cloud Storage Setup

Similar to S3, you need to make the uploaded object publicly readable. You can set this during upload or configure a default bucket policy for public access.

Code Example (Python with google-cloud-storage)

from google.cloud import storage
import urllib.parse

storage_client = storage.Client()
bucket_name = "your-bucket-name"
local_file_path = "path/to/your/local/file.jpg"
object_key = "folder/your-file-name.jpg"

bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(object_key)

# Upload file and make it public
blob.upload_from_filename(local_file_path)
blob.make_public()

# Get the auto-generated public URL (or construct it manually if preferred)
public_url = blob.public_url
# Alternatively, construct it: https://storage.googleapis.com/{bucket_name}/{encoded_key}
encoded_key = urllib.parse.quote(object_key)
manual_public_url = f"https://storage.googleapis.com/{bucket_name}/{encoded_key}"

# Store either URL in your database
print(public_url)

Azure Blob Storage Setup

For Azure, you'll need to set your container to allow anonymous public read access (for blob-level access) if you want permanent public URLs without pre-signing. Alternatively, you can set individual blob permissions, but container-wide public access is simpler for this use case.

Code Example (Python with azure-storage-blob)

from azure.storage.blob import BlobServiceClient
import urllib.parse

connection_string = "your-azure-storage-connection-string"
container_name = "your-container-name"
local_file_path = "path/to/your/local/file.jpg"
blob_name = "folder/your-file-name.jpg"

blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(container_name)

# Upload the file to the blob container
with open(local_file_path, "rb") as data:
    container_client.upload_blob(name=blob_name, data=data, overwrite=True)

# Encode blob name for special characters
encoded_blob_name = urllib.parse.quote(blob_name)
# Construct the public HTTPS URL
public_url = f"https://{blob_service_client.account_name}.blob.core.windows.net/{container_name}/{encoded_blob_name}"

# Store this URL in your database
print(public_url)

Key General Tips

  • Double-check Permissions: Always test the generated URL in a private browser window to confirm it's publicly accessible—nothing's worse than storing a URL that returns a 403 error!
  • URL Encoding: Never skip encoding the object/blob key if it might contain spaces, non-ASCII characters, or symbols. This ensures the URL works correctly across all browsers and systems.
  • Region/Endpoint Variations: Cloud providers have different URL formats for different regions. Make sure your URL template matches the region where your bucket/container is hosted.

内容的提问来源于stack exchange,提问作者HyperDevil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:37:32