Skip to main content

ResQ.Storage.PinataClient

PinataClient.UploadAsync Method

PinataClient.UploadAsync(byte[], string, string, Dictionary<string,string>, CancellationToken) Method

Uploads binary data to IPFS and pins it.
public System.Threading.Tasks.Task<ResQ.Storage.UploadResult> UploadAsync(byte[] data, string fileName, string contentType, System.Collections.Generic.Dictionary<string,string>? metadata=null, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

data System.Byte[] The file content as a byte array. fileName System.String The desired filename for the uploaded content. contentType System.String The MIME type of the content. metadata System.Collections.Generic.Dictionary<System.String,System.String> Optional dictionary of custom metadata key-value pairs. cancellationToken System.Threading.CancellationToken Cancellation token for the operation. Implements UploadAsync(byte[], string, string, Dictionary<string,string>, CancellationToken)

Returns

System.Threading.Tasks.Task<UploadResult>
An UploadResult containing the CID and upload metadata.

Exceptions

System.ArgumentNullException
Thrown when data, fileName, or contentType is null.
System.InvalidOperationException
Thrown when the upload fails.
System.OperationCanceledException
Thrown when the operation is cancelled.

Example

byte[] imageBytes = await File.ReadAllBytesAsync("photo.jpg");
var result = await storage.UploadAsync(
    imageBytes,
    "photo.jpg",
    "image/jpeg");

Remarks

This is a convenience overload that wraps the byte array in a MemoryStream and calls UploadAsync(Stream, string, string, Dictionary<string,string>, CancellationToken).

PinataClient.UploadAsync(Stream, string, string, Dictionary<string,string>, CancellationToken) Method

Uploads a file stream to IPFS and pins it.
public System.Threading.Tasks.Task<ResQ.Storage.UploadResult> UploadAsync(System.IO.Stream content, string fileName, string contentType, System.Collections.Generic.Dictionary<string,string>? metadata=null, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

content System.IO.Stream The file content as a stream. The stream will be read to completion. fileName System.String The desired filename for the uploaded content. contentType System.String The MIME type of the content (e.g., “image/jpeg”, “application/pdf”). metadata System.Collections.Generic.Dictionary<System.String,System.String> Optional dictionary of custom metadata key-value pairs to attach to the pin. cancellationToken System.Threading.CancellationToken Cancellation token for the operation. Implements UploadAsync(Stream, string, string, Dictionary<string,string>, CancellationToken)

Returns

System.Threading.Tasks.Task<UploadResult>
An UploadResult containing the CID and upload metadata.

Exceptions

System.ArgumentNullException
Thrown when content, fileName, or contentType is null.
System.InvalidOperationException
Thrown when the upload fails.
System.OperationCanceledException
Thrown when the operation is cancelled.

Example

using var stream = File.OpenRead("photo.jpg");
var result = await storage.UploadAsync(
    stream,
    "photo.jpg",
    "image/jpeg",
    new Dictionary<string, string> { ["source"] = "drone-001" });
Console.WriteLine($"Uploaded: {result.Cid}");

Remarks

The stream will be read and the content uploaded to IPFS via Pinata. Once uploaded, the content is automatically pinned to ensure persistence. The returned CID can be used to retrieve the content from any IPFS gateway.