79825843

Date: 2025-11-20 18:42:48
Score: 5.5
Natty:
Report link

i have the same issue but with C# i dont know how to fix this

      System.Exception: OBS error:  - 
       ---> nodename nor servname provided, or not known (actas-digitales.obs.la-north-2.myhuaweicloud.com:443), StatusCode:0, ErrorCode:, ErrorMessage:, RequestId:, HostId:

This is my code

using Application.Common.Services.Interfaces;
using Application.Common.Utils;
using Microsoft.Extensions.Configuration;
using OBS;
using OBS.Model;
using System.IO;
using System.Threading.Tasks;

using Application.Common.Services.Interfaces;
using Application.Common.Utils;
using Microsoft.Extensions.Configuration;
using OBS;
using OBS.Model;
using System.IO;
using System.Threading.Tasks;

namespace Application.Common.Services
{
    public class HuaweiCloudService : IHuaweiCloudService
    {
        private readonly ObsClient _obsClient;

        private readonly string _accessKey;
        private readonly string _secretKey;
        private readonly string _endPoint;
        private readonly string _bucket;
        private readonly string _basePath;

        public HuaweiCloudService(IConfiguration config)
        {
            var section = config.GetSection("OBS");

            _accessKey = section["AccessKey"];
            _secretKey = section["SecretKey"];
            _endPoint  = section["EndPoint"];
            _bucket    = section["Bucket"];
            _basePath  = section["BasePath"];

            // Validate configuration
            if (string.IsNullOrWhiteSpace(_accessKey))
                throw new ArgumentException("OBS:AccessKey missing");

            if (string.IsNullOrWhiteSpace(_secretKey))
                throw new ArgumentException("OBS:SecretKey missing");

            if (string.IsNullOrWhiteSpace(_endPoint))
                throw new ArgumentException("OBS:EndPoint missing");

            if (string.IsNullOrWhiteSpace(_bucket))
                throw new ArgumentException("OBS:Bucket missing");


            // Initialize OBS client
            _obsClient = new ObsClient(
                _accessKey, 
                _secretKey, 
                new ObsConfig { Endpoint = _endPoint }
            );
        }

        /// <summary>
        /// Uploads a file to Huawei OBS
        /// </summary>
        public async Task<string> UploadAsync(Stream fileStream, string fileName, string contentType)
        {
            if (fileStream == null || fileStream.Length == 0)
                throw new ArgumentException("Stream is empty.", nameof(fileStream));

            var objectKey =
                string.IsNullOrWhiteSpace(_basePath)
                    ? fileName
                    : $"{_basePath.TrimEnd('/')}/{fileName}";

            var request = new PutObjectRequest
            {
                BucketName = _bucket,
                ObjectKey = objectKey,
                InputStream = fileStream,
                ContentType = contentType
            };

            try
            {
                var response = await Task.Run(() => _obsClient.PutObject(request));

                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                    throw new Exception($"OBS upload failed. Status: {response.StatusCode}");

                return objectKey;
            }
            catch (ObsException ex)
            {
                throw new Exception($"OBS error: {ex.ErrorCode} - {ex.ErrorMessage}", ex);
            }
        }

        public string GetPublicUrl(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentException("File key cannot be empty.", nameof(key));

            var endpoint = _endPoint
                .Replace("https://", "")
                .Replace("http://", "")
                .TrimEnd('/');

            return $"https://{_bucket}.{endpoint}/{key}";
        }

        /// <summary>
        /// Approve record by uploading file to OBS
        /// </summary>
        public async Task ApproveRecordAsync(
            int recordId,
            Stream fileStream,
            string fileName,
            string contentType,
            long length)
        {
            var ext = Path.GetExtension(fileName);
            var recordType = "nt3m";

            var key = StorageKeyGenerator.BuildKey(recordType, recordId, ext);

            await UploadAsync(fileStream, key, contentType);

            /*
            record.Type = recordType;
            record.StorageKey = key;
            record.State = "APROBADA";
            record.Url = $"";

            await _repo.UpdateAsync(record);
            */
        }
    }
}
Reasons:
  • Blacklisted phrase (1): i have the same issue
  • RegEx Blacklisted phrase (2): i dont know how to fix
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): i have the same issue
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: user31916738