Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `presigned_url_upload.py` — Generate a signed URL to upload a file to OSS securely
30 changes: 30 additions & 0 deletions examples/presigned_url_upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# examples/presigned_url_upload.py

import oss2

# Replace with your actual credentials and bucket info
ACCESS_KEY_ID = '<your-access-key-id>'
ACCESS_KEY_SECRET = '<your-access-key-secret>'
ENDPOINT = 'https://oss-cn-hangzhou.aliyuncs.com' # Change if needed
BUCKET_NAME = '<your-bucket-name>'
OBJECT_NAME = 'demo_upload_via_presigned.txt'
EXPIRATION_SECONDS = 300 # 5 minutes

# Initialize auth and bucket
auth = oss2.Auth(ACCESS_KEY_ID, ACCESS_KEY_SECRET)
bucket = oss2.Bucket(auth, ENDPOINT, BUCKET_NAME)

# Generate presigned URL for PUT upload
signed_url = bucket.sign_url('PUT', OBJECT_NAME, EXPIRATION_SECONDS)

print("✅ Presigned URL (valid for 5 minutes):")
print(signed_url)

# Optional upload using requests
try:
import requests
with open('example.txt', 'rb') as f:
response = requests.put(signed_url, data=f)
print("Upload status code:", response.status_code)
except Exception as e:
print("Install `requests` to auto-upload, or use the URL manually.")