Function Compute and OSS Integration
This code demonstrates how to integrate Alibaba Cloud Function Compute with OSS (Object Storage Service). It downloads a file from a specified URL and uploads it to an OSS bucket.
Code
# -*- coding: utf-8 -*-
import oss2
import requests
import logging
import os
def handler(event, context):
# Configuration
# model_url = "https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.2-GGUF/resolve/main/mistral-7b-instruct-v0.2.Q4_K_M.gguf"
model_url = "https://storage.googleapis.com/lzwjava1/ss_urls.txt"
oss_bucket_name = "lzw-download" # Your bucket name
oss_endpoint = "oss-cn-hangzhou.aliyuncs.com" # Public endpoint
try:
# Use the provided role ARN to create the auth object
role_arn = "acs:ram::1768386076294612:role/fc"
auth = oss2.Auth(
os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
)
sts_auth = oss2.StsAuth(
os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
os.environ['ALIBABA_CLOUD_SECURITY_TOKEN']
)
# 2. Initialize OSS client with proper endpoint
bucket = oss2.Bucket(
sts_auth,
oss_endpoint,
oss_bucket_name
)
# 3. Stream download & upload
logging.info("Starting download...")
with requests.get(model_url, stream=True) as response:
response.raise_for_status()
# Upload to OSS
result = bucket.put_object(
'models/mistral-7b-instruct-v0.2.Q4_K_M.gguf',
response.iter_content(chunk_size=1024*1024)
)
if result.status == 200:
logging.info("Upload succeeded!")
return "Success"
else:
raise Exception(f"OSS upload failed: {result.status}")
except Exception as e:
logging.error(f"Error: {str(e)}")
raise e
Error
We encountered the same error as before. It appears a VPN is required to complete this task.
2025-01-24 17:43:342025-01-24 17:43:34 1-679360a6-154b432a-15cadcce0c27 [ERROR] { “errorMessage”: “HTTPSConnectionPool(host=’huggingface.co’, port=443): Max retries exceeded with url: /TheBloke/Mistral-7B-Instruct-v0.2-GGUF/resolve/main/mistral-7b-instruct-v0.2.Q4_K_M.gguf (Caused by NewConnectionError(‘<urllib3.connection.HTTPSConnection object at 0x7f7d3c289f60>: Failed to establish a new connection: [Errno 101] Network is unreachable’))”