ba4be087d5
Create PR to main with cherry-pick from release / cherry-pick (push) Failing after 0s
CICD NeMo / pre-flight (push) Failing after 0s
CICD NeMo / configure (push) Has been skipped
Build, validate, and release Neural Modules / pre-flight (push) Failing after 1s
CICD NeMo / code-linting (push) Has been skipped
Build, validate, and release Neural Modules / release (push) Has been skipped
Build, validate, and release Neural Modules / release-summary (push) Has been cancelled
CICD NeMo / cicd-test-container-build (push) Has been cancelled
CICD NeMo / cicd-import-tests (push) Has been cancelled
CICD NeMo / L0_Setup_Test_Data_And_Models (push) Has been cancelled
CICD NeMo / cicd-main-unit-tests (push) Has been cancelled
CICD NeMo / cicd-main-speech (push) Has been cancelled
CICD NeMo / Nemo_CICD_Test (push) Has been cancelled
CICD NeMo / Coverage (e2e) (push) Has been cancelled
CICD NeMo / Coverage (unit-test) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
CICD NeMo / cicd-wait-in-queue (push) Has been cancelled
136 lines
3.9 KiB
Python
136 lines
3.9 KiB
Python
# Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
from dataclasses import dataclass
|
|
from typing import BinaryIO, Optional
|
|
|
|
import boto3
|
|
from botocore.config import Config
|
|
|
|
|
|
@dataclass
|
|
class S3Config:
|
|
"""Configuration required to connect to the target S3-compatible storage."""
|
|
|
|
bucket: str
|
|
endpoint_url: str
|
|
region_name: str
|
|
connect_timeout: int = 10
|
|
|
|
|
|
class S3Client:
|
|
"""Client for uploading report artifacts to S3-compatible object storage."""
|
|
|
|
def __init__(
|
|
self,
|
|
cfg: S3Config,
|
|
aws_access_key_id: str,
|
|
aws_secret_access_key: str,
|
|
) -> None:
|
|
self.cfg = cfg
|
|
self.client = boto3.client(
|
|
"s3",
|
|
endpoint_url=cfg.endpoint_url,
|
|
aws_access_key_id=aws_access_key_id,
|
|
aws_secret_access_key=aws_secret_access_key,
|
|
region_name=cfg.region_name,
|
|
config=Config(connect_timeout=cfg.connect_timeout),
|
|
)
|
|
|
|
def upload_fileobj(
|
|
self,
|
|
fileobj: BinaryIO,
|
|
key: str,
|
|
expires_in: int,
|
|
content_type: Optional[str] = None,
|
|
) -> str:
|
|
"""Upload a binary file-like object and return a presigned download URL.
|
|
|
|
Args:
|
|
fileobj: File-like object to upload.
|
|
key: S3 object key for the uploaded file.
|
|
expires_in: Lifetime of the presigned URL in seconds.
|
|
content_type: Optional content type stored with the uploaded object.
|
|
|
|
Returns:
|
|
Presigned URL for downloading the uploaded object.
|
|
"""
|
|
kwargs = {
|
|
"Fileobj": fileobj,
|
|
"Bucket": self.cfg.bucket,
|
|
"Key": key,
|
|
}
|
|
|
|
if content_type is not None:
|
|
kwargs["ExtraArgs"] = {"ContentType": content_type}
|
|
|
|
self.client.upload_fileobj(**kwargs)
|
|
|
|
return self.get_presigned_url(key, expires_in)
|
|
|
|
def upload_bytes(
|
|
self,
|
|
data: bytes,
|
|
key: str,
|
|
expires_in: int,
|
|
content_type: Optional[str] = None,
|
|
) -> str:
|
|
"""Upload raw bytes and return a presigned download URL.
|
|
|
|
Args:
|
|
data: File content to upload.
|
|
key: S3 object key for the uploaded file.
|
|
expires_in: Lifetime of the presigned URL in seconds.
|
|
content_type: Optional content type stored with the uploaded object.
|
|
|
|
Returns:
|
|
Presigned URL for downloading the uploaded object.
|
|
"""
|
|
extra_args = {}
|
|
|
|
if content_type is not None:
|
|
extra_args["ContentType"] = content_type
|
|
|
|
self.client.put_object(
|
|
Bucket=self.cfg.bucket,
|
|
Key=key,
|
|
Body=data,
|
|
**extra_args,
|
|
)
|
|
|
|
return self.get_presigned_url(key, expires_in)
|
|
|
|
def get_presigned_url(
|
|
self,
|
|
key: str,
|
|
expires_in: int,
|
|
) -> str:
|
|
"""Generate a presigned download URL for an uploaded object.
|
|
|
|
Args:
|
|
key: S3 object key.
|
|
expires_in: Lifetime of the presigned URL in seconds.
|
|
|
|
Returns:
|
|
Presigned URL for the requested object.
|
|
"""
|
|
return self.client.generate_presigned_url(
|
|
"get_object",
|
|
Params={"Bucket": self.cfg.bucket, "Key": key},
|
|
ExpiresIn=expires_in,
|
|
)
|
|
|
|
def close(self) -> None:
|
|
"""Close the underlying S3 client."""
|
|
self.client.close()
|