oracle / oci-dotnet-sdk

Oracle Cloud Infrastructure SDK for .NET
https://cloud.oracle.com/cloud-infrastructure
Other
53 stars 20 forks source link

Nonseekable stream can not be uploaded to Object storage #214

Open jeffhli opened 7 months ago

jeffhli commented 7 months ago

The following code will throw an exception that Oci.Common.Model.OciException HResult=0x80131500 Message=v2-chunked-transfer-encoding Source=OCI.DotNetSDK.Common StackTrace: at Oci.Common.Http.Internal.ResponseHelper.HandleNonSuccessfulResponse(HttpResponseMessage responseMessage, ApiDetails apiDetails, String caller) at Oci.Common.Http.RestClient.CheckHttpResponseMessage(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse, ApiDetails apiDetails) at Oci.ObjectstorageService.ObjectStorageClient.d48.MoveNext() at Program.<

$>d0.MoveNext() in D:\code\localtest\OraclePlayground\Program.cs:line 37

  • NonseekableStream

    internal class NonseekableStream : Stream
    {
        private Stream innerStream;
    
        public NonseekableStream(Stream innerStream)
        {
            this.innerStream = innerStream;
        }
    
        public override bool CanRead => innerStream.CanRead;
    
        public override bool CanSeek => false;
    
        public override bool CanWrite => false;
    
        public override long Length => innerStream.Length;
    
        public override long Position { get => innerStream.Position; set => throw new NotSupportedException(); }
    
        public override void Flush()
        {
            throw new NotSupportedException();
        }
    
        public override int Read(byte[] buffer, int offset, int count)
        {
            return innerStream.Read(buffer, offset, count);
        }
    
        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotSupportedException();
        }
    
        public override void SetLength(long value)
        {
            throw new NotSupportedException();
        }
    
        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException();
        }
    }
  • Main code
    
    var objectStorageClient = new ObjectStorageClient(provider);
    var uri = objectStorageClient.GetEndpoint();
    var bucketResponse = await objectStorageClient.GetBucket(new GetBucketRequest()
    {
    NamespaceName = "ax1hhmmvkvpo",
    BucketName = "jefftest"
    });
    var bucket = bucketResponse.Bucket;
    var putObjectResponse = await objectStorageClient.PutObject(new PutObjectRequest()
    {
    NamespaceName = "ax1hhkmvkvpo",
    BucketName = "jefftest",
    PutObjectBody = new NonseekableStream(new MemoryStream(Encoding.UTF8.GetBytes("Hello, World2!"))),
    ObjectName = "test.txt",

});