Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Supabase.sln
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gotrue", "packages\Gotrue\G
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gotrue.Tests", "packages\Gotrue\Gotrue.Tests\Gotrue.Tests.csproj", "{5BF53305-E4EE-4313-9840-2CB693560A7D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Storage", "Storage", "{5FF951CE-4342-0718-79D4-E9BFE5069FF3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Storage", "packages\Storage\Storage\Storage.csproj", "{CD9D9CBA-6916-4764-823A-416D7020F8DA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Storage.Tests", "packages\Storage\Storage.Tests\Storage.Tests.csproj", "{A5F428CE-EC28-4A71-A90E-226E91957FC1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -175,6 +181,30 @@ Global
{5BF53305-E4EE-4313-9840-2CB693560A7D}.Release|x64.Build.0 = Release|Any CPU
{5BF53305-E4EE-4313-9840-2CB693560A7D}.Release|x86.ActiveCfg = Release|Any CPU
{5BF53305-E4EE-4313-9840-2CB693560A7D}.Release|x86.Build.0 = Release|Any CPU
{CD9D9CBA-6916-4764-823A-416D7020F8DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CD9D9CBA-6916-4764-823A-416D7020F8DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD9D9CBA-6916-4764-823A-416D7020F8DA}.Debug|x64.ActiveCfg = Debug|Any CPU
{CD9D9CBA-6916-4764-823A-416D7020F8DA}.Debug|x64.Build.0 = Debug|Any CPU
{CD9D9CBA-6916-4764-823A-416D7020F8DA}.Debug|x86.ActiveCfg = Debug|Any CPU
{CD9D9CBA-6916-4764-823A-416D7020F8DA}.Debug|x86.Build.0 = Debug|Any CPU
{CD9D9CBA-6916-4764-823A-416D7020F8DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD9D9CBA-6916-4764-823A-416D7020F8DA}.Release|Any CPU.Build.0 = Release|Any CPU
{CD9D9CBA-6916-4764-823A-416D7020F8DA}.Release|x64.ActiveCfg = Release|Any CPU
{CD9D9CBA-6916-4764-823A-416D7020F8DA}.Release|x64.Build.0 = Release|Any CPU
{CD9D9CBA-6916-4764-823A-416D7020F8DA}.Release|x86.ActiveCfg = Release|Any CPU
{CD9D9CBA-6916-4764-823A-416D7020F8DA}.Release|x86.Build.0 = Release|Any CPU
{A5F428CE-EC28-4A71-A90E-226E91957FC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A5F428CE-EC28-4A71-A90E-226E91957FC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5F428CE-EC28-4A71-A90E-226E91957FC1}.Debug|x64.ActiveCfg = Debug|Any CPU
{A5F428CE-EC28-4A71-A90E-226E91957FC1}.Debug|x64.Build.0 = Debug|Any CPU
{A5F428CE-EC28-4A71-A90E-226E91957FC1}.Debug|x86.ActiveCfg = Debug|Any CPU
{A5F428CE-EC28-4A71-A90E-226E91957FC1}.Debug|x86.Build.0 = Debug|Any CPU
{A5F428CE-EC28-4A71-A90E-226E91957FC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5F428CE-EC28-4A71-A90E-226E91957FC1}.Release|Any CPU.Build.0 = Release|Any CPU
{A5F428CE-EC28-4A71-A90E-226E91957FC1}.Release|x64.ActiveCfg = Release|Any CPU
{A5F428CE-EC28-4A71-A90E-226E91957FC1}.Release|x64.Build.0 = Release|Any CPU
{A5F428CE-EC28-4A71-A90E-226E91957FC1}.Release|x86.ActiveCfg = Release|Any CPU
{A5F428CE-EC28-4A71-A90E-226E91957FC1}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -190,6 +220,9 @@ Global
{C3D77741-3A1A-DB3D-37EC-B7F1BAA29ACA} = {809F86A1-1C4C-B159-0CD4-DF9D33D876CE}
{900A499A-A587-4036-B995-5E1E123241E0} = {C3D77741-3A1A-DB3D-37EC-B7F1BAA29ACA}
{5BF53305-E4EE-4313-9840-2CB693560A7D} = {C3D77741-3A1A-DB3D-37EC-B7F1BAA29ACA}
{5FF951CE-4342-0718-79D4-E9BFE5069FF3} = {809F86A1-1C4C-B159-0CD4-DF9D33D876CE}
{CD9D9CBA-6916-4764-823A-416D7020F8DA} = {5FF951CE-4342-0718-79D4-E9BFE5069FF3}
{A5F428CE-EC28-4A71-A90E-226E91957FC1} = {5FF951CE-4342-0718-79D4-E9BFE5069FF3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {832DE89D-7252-4B03-9301-BB8D36B40992}
Expand Down
116 changes: 116 additions & 0 deletions packages/Storage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Supabase.Storage

[![Build and Test](https://github.com/supabase-community/storage-csharp/actions/workflows/build-and-test.yml/badge.svg)](https://github.com/supabase-community/storage-csharp/acionts/workflows/build-and-test.yml)
[![NuGet](https://img.shields.io/nuget/vpre/Supabase.Storage")](https://www.nuget.org/packages/Supabase.Storage/)

---

Integrate your [Supabase](https://supabase.io) projects with C#.

## [Notice]: v2.0.0 renames this package from `storage-csharp` to `Supabase.Storage`. The depreciation notice has been set in NuGet. The API remains the same.

## Examples (using supabase-csharp)

```c#
public async void Main()
{
// Make sure you set these (or similar)
var url = Environment.GetEnvironmentVariable("SUPABASE_URL");
var key = Environment.GetEnvironmentVariable("SUPABASE_KEY");

await Supabase.Client.InitializeAsync(url, key);

// The Supabase Instance can be accessed at any time using:
// Supabase.Client.Instance {.Realtime|.Auth|etc.}
// For ease of readability we'll use this:
var instance = Supabase.Client.Instance;

// Interact with Supabase Storage
var storage = Supabase.Client.Instance.Storage
await storage.CreateBucket("testing")

var bucket = storage.From("testing");

var basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace("file:", "");
var imagePath = Path.Combine(basePath, "Assets", "supabase-csharp.png");

await bucket.Upload(imagePath, "supabase-csharp.png");

// If bucket is public, get url
bucket.GetPublicUrl("supabase-csharp.png");

// If bucket is private, generate url
await bucket.CreateSignedUrl("supabase-csharp.png", 3600));

// Download it!
await bucket.Download("supabase-csharp.png", Path.Combine(basePath, "testing-download.png"));
}
```

## Observability (OpenTelemetry)

The client emits traces and metrics through `System.Diagnostics`, so you can wire them into
OpenTelemetry (or any `ActivityListener`/`MeterListener`) without the client taking a dependency
on the OpenTelemetry packages. Emission is zero-cost while nothing is listening, so it is always
on and stays silent until you subscribe.

Register the client's `ActivitySource` and `Meter` by name. Use the `StorageDiagnostics.SourceName`
constant rather than hardcoding the string, so a typo becomes a compile error instead of a silent
no-op:

```csharp
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
using Supabase.Storage;

// Requires the OpenTelemetry.Extensions.Hosting and an exporter package (e.g. OTLP) in your app.
builder.Services.AddOpenTelemetry()
.WithTracing(tracing => tracing
.AddSource(StorageDiagnostics.SourceName)
.AddOtlpExporter())
.WithMetrics(metrics => metrics
.AddMeter(StorageDiagnostics.SourceName)
.AddOtlpExporter());
```

Once subscribed you get:

- A client span per request, named `{METHOD} {path}` and following OpenTelemetry HTTP conventions
(method, status code, and a sanitized URL). The query string is **never** recorded — Storage
signed URLs carry a `token` there. Upload and download spans additionally carry a
`storage.transfer.direction` tag (`upload` / `download`). The resumable (TUS) upload is reported
as a single operation span covering the whole transfer, rather than one span per underlying chunk
request.
- `supabase.storage.http.request.duration` (seconds) for control-plane requests.
- `supabase.storage.transfer.duration` (seconds) and `supabase.storage.transfer.size` (bytes) for
uploads and downloads, tagged by direction — because a duration alone does not describe a file
transfer.

If you are not using the OpenTelemetry SDK, a raw listener works too:

```csharp
using System.Diagnostics;
using Supabase.Storage;

using var listener = new ActivityListener
{
ShouldListenTo = source => source.Name == StorageDiagnostics.SourceName,
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData,
ActivityStopped = activity => Console.WriteLine($"{activity.OperationName} {activity.Duration.TotalMilliseconds}ms {activity.Status}")
};
ActivitySource.AddActivityListener(listener);
```

## Package made possible through the efforts of:

Join the ranks! See a problem? Help fix it!

<a href="https://github.com/supabase-community/storage-csharp/graphs/contributors">
<img src="https://contrib.rocks/image?repo=supabase-community/storage-csharp" />
</a>

Made with [contrib.rocks](https://contrib.rocks/preview?repo=supabase-community%storage-csharp).

## Contributing

We are more than happy to have contributions! Please submit a PR.
86 changes: 86 additions & 0 deletions packages/Storage/Storage.Tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
root = true

[*]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 4

[*.cs]
# --- Language style ---
csharp_preferred_modifier_order = public, private, protected, internal, new, static, abstract, virtual, sealed, readonly, override, extern, unsafe, volatile, async, file, required:suggestion
csharp_space_after_cast = true
csharp_new_line_before_members_in_object_initializers = false
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_var_when_type_is_apparent = true:suggestion
csharp_style_var_elsewhere = true:suggestion

dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion
dotnet_style_qualification_for_field = true:suggestion
dotnet_style_qualification_for_property = true:suggestion
dotnet_style_qualification_for_method = true:suggestion
dotnet_style_qualification_for_event = true:suggestion
dotnet_style_require_accessibility_modifiers = for_non_interface_members:suggestion

# --- Expression-bodied members: use an expression body when the member is a single statement ---
csharp_style_expression_bodied_methods = when_on_single_line:suggestion
csharp_style_expression_bodied_constructors = when_on_single_line:suggestion
csharp_style_expression_bodied_operators = when_on_single_line:suggestion
csharp_style_expression_bodied_local_functions = when_on_single_line:suggestion
csharp_style_expression_bodied_lambdas = when_on_single_line:suggestion
csharp_style_expression_bodied_properties = when_on_single_line:suggestion
csharp_style_expression_bodied_indexers = when_on_single_line:suggestion
csharp_style_expression_bodied_accessors = when_on_single_line:suggestion

# --- Naming (warning-level) ---
# private instance / static fields: camelCase, NO underscore prefix (CONVENTIONS §4)
dotnet_naming_rule.private_fields_camel_case.severity = warning
dotnet_naming_rule.private_fields_camel_case.symbols = private_fields
dotnet_naming_rule.private_fields_camel_case.style = camel_case
dotnet_naming_symbols.private_fields.applicable_kinds = field
dotnet_naming_symbols.private_fields.applicable_accessibilities = private

# private const: PascalCase (declared before the general rule so it wins)
dotnet_naming_rule.private_const_pascal_case.severity = warning
dotnet_naming_rule.private_const_pascal_case.symbols = private_const_fields
dotnet_naming_rule.private_const_pascal_case.style = pascal_case
dotnet_naming_symbols.private_const_fields.applicable_kinds = field
dotnet_naming_symbols.private_const_fields.applicable_accessibilities = private
dotnet_naming_symbols.private_const_fields.required_modifiers = const

# private static readonly: PascalCase
dotnet_naming_rule.private_static_readonly_pascal_case.severity = warning
dotnet_naming_rule.private_static_readonly_pascal_case.symbols = private_static_readonly_fields
dotnet_naming_rule.private_static_readonly_pascal_case.style = pascal_case
dotnet_naming_symbols.private_static_readonly_fields.applicable_kinds = field
dotnet_naming_symbols.private_static_readonly_fields.applicable_accessibilities = private
dotnet_naming_symbols.private_static_readonly_fields.required_modifiers = static, readonly

dotnet_naming_style.camel_case.capitalization = camel_case
dotnet_naming_style.pascal_case.capitalization = pascal_case

# --- ReSharper / Rider hints (IDE-only; not enforced by dotnet build) ---
# Collapse blank lines in method bodies to 0 — backs the "no blank lines between arrange/act/assert"
# rule. Rider-enforced; CI cannot enforce this via editorconfig alone.
resharper_csharp_keep_blank_lines_in_code = 0
resharper_csharp_keep_blank_lines_in_declarations = 1
resharper_braces_for_ifelse = required
resharper_braces_for_for = required
resharper_braces_for_foreach = required
resharper_braces_for_while = required
resharper_trailing_comma_in_multiline_lists = true

[*.{csproj,props,targets,nuspec}]
indent_size = 2

[*.{json,jsonc}]
indent_size = 2

[*.{yaml,yml}]
indent_size = 2

[*.{bash,sh,zsh}]
indent_size = 2
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 82 additions & 0 deletions packages/Storage/Storage.Tests/Buckets/StorageBucketAnonTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Storage.Tests;
using Supabase.Storage;
using Supabase.Storage.Exceptions;

namespace Storage.Tests.Buckets;

/// <summary>
/// End-to-end tests asserting the anon (public-key) client is denied bucket administration against a
/// running local Supabase stack: listing yields nothing, getting an existing bucket resolves to null,
/// and create/update/empty/delete all surface a <see cref="SupabaseStorageException"/>.
/// </summary>
[TestClass]
[TestCategory("E2E")]
public class StorageBucketAnonTests
{
private Client AdminStorage => Helpers.GetServiceClient();
private Client Storage => Helpers.GetPublicClient();

[TestMethod]
public async Task ListBuckets_ShouldBeEmptyAndDenyUpload()
{
(await this.Storage.ListBuckets()).Should().BeEmpty();
var act = async () =>
{
var newParentBucket = "parent";
if (await this.AdminStorage.GetBucket("parent") == null)
newParentBucket = await this.AdminStorage.CreateBucket("parent");
await this.Storage.From(newParentBucket).Upload(new byte[] { 0x0, 0x0, 0x0 }, "child/test.bin");
};
await act.Should().ThrowAsync<SupabaseStorageException>();
}

[TestMethod]
public async Task GetBucket_ShouldReturnNull_GivenNoPermission()
{
var id = Guid.NewGuid().ToString();
await this.AdminStorage.CreateBucket(id);
(await this.Storage.GetBucket(id)).Should().BeNull();
await this.AdminStorage.DeleteBucket(id);
}

[TestMethod]
public async Task CreateBucket_ShouldThrow_GivenNoPermission()
{
var act = () => this.Storage.CreateBucket("parent");
await act.Should().ThrowAsync<SupabaseStorageException>();
}

[TestMethod]
public async Task UpdateBucket_ShouldThrow_GivenNoPermission()
{
var id = Guid.NewGuid().ToString();
await this.AdminStorage.CreateBucket(id);
var act = () => this.Storage.UpdateBucket(id, new BucketUpsertOptions { Public = true });
await act.Should().ThrowAsync<SupabaseStorageException>();
await this.AdminStorage.DeleteBucket(id);
}

[TestMethod]
public async Task EmptyBucket_ShouldThrow_GivenNoPermission()
{
var id = Guid.NewGuid().ToString();
await this.AdminStorage.CreateBucket(id);
var act = () => this.Storage.EmptyBucket(id);
await act.Should().ThrowAsync<SupabaseStorageException>();
await this.AdminStorage.DeleteBucket(id);
}

[TestMethod]
public async Task DeleteBucket_ShouldThrow_GivenNoPermission()
{
var id = Guid.NewGuid().ToString();
await this.AdminStorage.CreateBucket(id);
var act = () => this.Storage.DeleteBucket(id);
await act.Should().ThrowAsync<SupabaseStorageException>();
await this.AdminStorage.DeleteBucket(id);
}
}
Loading
Loading