Funktionsberechnung und OSS-Integration | Original, von KI übersetzt
Dieser Code demonstriert die Integration von Alibaba Cloud Function Compute mit OSS (Object Storage Service). Er lädt eine Datei von einer angegebenen URL herunter und lädt sie in einen OSS-Bucket hoch.
Code
# -*- coding: utf-8 -*-
import oss2
import requests
import logging
import os
def handler(event, context):
# Konfiguration
# 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" # Ihr Bucket-Name
oss_endpoint = "oss-cn-hangzhou.aliyuncs.com" # Öffentlicher Endpoint
try:
# Verwenden Sie die bereitgestellte Rollen-ARN, um das Auth-Objekt zu erstellen
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. Initialisieren Sie den OSS-Client mit dem richtigen Endpoint
bucket = oss2.Bucket(
sts_auth,
oss_endpoint,
oss_bucket_name
)
# 3. Stream-Download & Upload
logging.info("Download wird gestartet...")
with requests.get(model_url, stream=True) as response:
response.raise_for_status()
# Upload nach 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 erfolgreich!")
return "Success"
else:
raise Exception(f"OSS-Upload fehlgeschlagen: {result.status}")
except Exception as e:
logging.error(f"Fehler: {str(e)}")
raise e
Fehler
Wir haben denselben Fehler wie zuvor festgestellt. Es scheint, dass ein VPN erforderlich ist, um diese Aufgabe abzuschließen.
2025-01-24 17:43:342025-01-24 17:43:34 1-679360a6-154b432a-15cadcce0c27 [FEHLER] { “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’))”