1
This commit is contained in:
120
backend/tools.py
120
backend/tools.py
@@ -232,77 +232,77 @@ def scheme_convert_to_json():
|
||||
"""
|
||||
|
||||
# def send_instruction_to_robot_platform():
|
||||
"""从S3获取最新的json文件并返回其URL链接
|
||||
# """从S3获取最新的json文件并返回其URL链接
|
||||
|
||||
Returns:
|
||||
str: 最新json文件的预签名URL
|
||||
"""
|
||||
import boto3
|
||||
from botocore.exceptions import NoCredentialsError
|
||||
# Returns:
|
||||
# str: 最新json文件的预签名URL
|
||||
# """
|
||||
# import boto3
|
||||
# from botocore.exceptions import NoCredentialsError
|
||||
|
||||
# S3配置
|
||||
endpoint_url = "http://100.85.52.31:9000" or "https://s3-api.siat-mic.com"
|
||||
aws_access_key_id = "9bUtQL1Gpo9JB6o3pSGr"
|
||||
aws_secret_access_key = "1Qug5H73R3kP8boIHvdVcFtcb1jU9GRWnlmMpx0g"
|
||||
bucket_name = "temp"
|
||||
# # S3配置
|
||||
# endpoint_url = "http://100.85.52.31:9000" or "https://s3-api.siat-mic.com"
|
||||
# aws_access_key_id = "9bUtQL1Gpo9JB6o3pSGr"
|
||||
# aws_secret_access_key = "1Qug5H73R3kP8boIHvdVcFtcb1jU9GRWnlmMpx0g"
|
||||
# bucket_name = "temp"
|
||||
|
||||
try:
|
||||
# 创建S3客户端
|
||||
s3 = boto3.client(
|
||||
's3',
|
||||
endpoint_url=endpoint_url,
|
||||
aws_access_key_id=aws_access_key_id,
|
||||
aws_secret_access_key=aws_secret_access_key
|
||||
)
|
||||
# try:
|
||||
# # 创建S3客户端
|
||||
# s3 = boto3.client(
|
||||
# 's3',
|
||||
# endpoint_url=endpoint_url,
|
||||
# aws_access_key_id=aws_access_key_id,
|
||||
# aws_secret_access_key=aws_secret_access_key
|
||||
# )
|
||||
|
||||
# 列出bucket中的所有json文件
|
||||
response = s3.list_objects_v2(
|
||||
Bucket=bucket_name,
|
||||
Prefix='',
|
||||
Delimiter='/'
|
||||
)
|
||||
# # 列出bucket中的所有json文件
|
||||
# response = s3.list_objects_v2(
|
||||
# Bucket=bucket_name,
|
||||
# Prefix='',
|
||||
# Delimiter='/'
|
||||
# )
|
||||
|
||||
# 过滤出json文件并按最后修改时间排序
|
||||
json_files = [
|
||||
obj for obj in response.get('Contents', [])
|
||||
if obj['Key'].endswith('.json')
|
||||
]
|
||||
json_files.sort(key=lambda x: x['LastModified'], reverse=True)
|
||||
# # 过滤出json文件并按最后修改时间排序
|
||||
# json_files = [
|
||||
# obj for obj in response.get('Contents', [])
|
||||
# if obj['Key'].endswith('.json')
|
||||
# ]
|
||||
# json_files.sort(key=lambda x: x['LastModified'], reverse=True)
|
||||
|
||||
if not json_files:
|
||||
return "No JSON files found in S3 bucket"
|
||||
# if not json_files:
|
||||
# return "No JSON files found in S3 bucket"
|
||||
|
||||
# 获取最新文件
|
||||
latest_file = json_files[0]
|
||||
# # 获取最新文件
|
||||
# latest_file = json_files[0]
|
||||
|
||||
# 生成预签名URL
|
||||
url = s3.generate_presigned_url(
|
||||
'get_object',
|
||||
Params={
|
||||
'Bucket': bucket_name,
|
||||
'Key': latest_file['Key']
|
||||
},
|
||||
ExpiresIn=3600 # URL有效期1小时
|
||||
)
|
||||
# # 生成预签名URL
|
||||
# url = s3.generate_presigned_url(
|
||||
# 'get_object',
|
||||
# Params={
|
||||
# 'Bucket': bucket_name,
|
||||
# 'Key': latest_file['Key']
|
||||
# },
|
||||
# ExpiresIn=3600 # URL有效期1小时
|
||||
# )
|
||||
|
||||
# 将内部URL转换为外部可访问URL
|
||||
external_url = url.replace("http://100.85.52.31:9000", "https://s3-api.siat-mic.com")
|
||||
# # 将内部URL转换为外部可访问URL
|
||||
# external_url = url.replace("http://100.85.52.31:9000", "https://s3-api.siat-mic.com")
|
||||
|
||||
# 发送URL到FastAPI服务器
|
||||
try:
|
||||
response = requests.post(
|
||||
"http://localhost:8030/receive",
|
||||
json={"url": external_url}
|
||||
)
|
||||
response.raise_for_status()
|
||||
return external_url
|
||||
except requests.exceptions.RequestException as e:
|
||||
return f"Error sending URL to server: {str(e)}"
|
||||
# # 发送URL到FastAPI服务器
|
||||
# try:
|
||||
# response = requests.post(
|
||||
# "http://localhost:8030/receive",
|
||||
# json={"url": external_url}
|
||||
# )
|
||||
# response.raise_for_status()
|
||||
# return external_url
|
||||
# except requests.exceptions.RequestException as e:
|
||||
# return f"Error sending URL to server: {str(e)}"
|
||||
|
||||
except NoCredentialsError:
|
||||
return "Credentials not available"
|
||||
except Exception as e:
|
||||
return f"Error: {str(e)}"
|
||||
# except NoCredentialsError:
|
||||
# return "Credentials not available"
|
||||
# except Exception as e:
|
||||
# return f"Error: {str(e)}"
|
||||
|
||||
|
||||
def upload_to_s3(json_data: str):
|
||||
|
||||
Reference in New Issue
Block a user