Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cloud storage function #285

Closed
wants to merge 7 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
<AssemblyOriginatorKeyFile>../../FirebaseAdmin.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<CodeAnalysisRuleSet>../../stylecop_test.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Apis.Auth" Version="1.40.0" />
<PackageReference Include="Google.Cloud.Storage.V1" Version="2.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2018, Google Inc. 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.

using System.IO;
using System.Text;
using FirebaseAdmin.Cloud;
using Google.Cloud.Storage.V1;
using Xunit;

namespace FirebaseAdmin.IntegrationTests
{
public class StorageClientHelperTest
{
public StorageClientHelperTest()
{
IntegrationTestUtils.EnsureDefaultApp();
}

[Fact]
public void UseBucket()
{
var storageClient = StorageClientHelper.GetStorageClient();
this.TestBucket(FirebaseApp.DefaultInstance.GetProjectId(), storageClient);
}

private void TestBucket(string projectId, StorageClient storageClient)
{
var bucketName = this.GetDefaultBucketName(projectId);

var fileName = "FirebaseStorageTest.txt";
var content = "FirebaseStorageTest";
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content)))
{
var obj1 = storageClient.UploadObject(bucketName, fileName, "text/plain", stream);
Assert.Equal(bucketName, obj1.Bucket);
}

using (var stream = new MemoryStream())
{
storageClient.DownloadObject(bucketName, fileName, stream);
Assert.Equal(content, Encoding.UTF8.GetString(stream.ToArray()));
}

storageClient.DeleteObject(bucketName, fileName);
}

private string GetDefaultBucketName(string projectId)
{
return projectId + ".appspot.com";
}
}
}
47 changes: 24 additions & 23 deletions FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseAdmin.Snippets.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<CodeAnalysisRuleSet>../../stylecop_test.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Apis.Auth" Version="1.49.0" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FirebaseAdmin\FirebaseAdmin.csproj" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.1-beta.61">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<CodeAnalysisRuleSet>../../stylecop_test.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Apis.Auth" Version="1.49.0" />
<PackageReference Include="Google.Cloud.Storage.V1" Version="2.2.1" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FirebaseAdmin\FirebaseAdmin.csproj" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.1-beta.61">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
68 changes: 68 additions & 0 deletions FirebaseAdmin/FirebaseAdmin.Tests/Cloud/StorageClientHelperTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2018, Google Inc. 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.

using System;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Storage.V1;
using Xunit;

namespace FirebaseAdmin.Cloud.Tests
{
public class StorageClientHelperTest : IDisposable
{
private static readonly GoogleCredential MockCredential =
GoogleCredential.FromAccessToken("test-token");

[Fact]
public void GetStorageClientWithoutApp()
{
Assert.Null(StorageClientHelper.GetStorageClient());
}

[Fact]
public void GetDefaultStorageClient()
{
var app = FirebaseApp.Create(new AppOptions() { Credential = MockCredential });
StorageClient storageClient = StorageClientHelper.GetStorageClient();
Assert.Same(storageClient, StorageClientHelper.GetStorageClient());
app.Delete();
Assert.Null(StorageClientHelper.GetStorageClient());
}

[Fact]
public void GetStorageClient()
{
var app = FirebaseApp.Create(new AppOptions() { Credential = MockCredential });
StorageClient storageClient = StorageClientHelper.GetStorageClient(app);
Assert.Same(storageClient, StorageClientHelper.GetStorageClient(app));
app.Delete();
Assert.Throws<InvalidOperationException>(() => StorageClientHelper.GetStorageClient(app));
}

[Fact]
public void UseAfterDelete()
{
var app = FirebaseApp.Create(new AppOptions() { Credential = MockCredential });
StorageClient storageClient = StorageClientHelper.GetStorageClient(app);
app.Delete();
Assert.Throws<ObjectDisposedException>(
() => storageClient.GetBucket("test"));
}

public void Dispose()
{
FirebaseApp.DeleteAll();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Google.Apis.Auth" Version="1.49.0" />
<PackageReference Include="Google.Cloud.Storage.V1" Version="2.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="System.Linq.Async" Version="4.1.1" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down
78 changes: 78 additions & 0 deletions FirebaseAdmin/FirebaseAdmin/Cloud/StorageClientHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2018, Google Inc. 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.

using System;
using Google.Cloud.Storage.V1;

namespace FirebaseAdmin.Cloud
{
/// <summary>
/// StorageClientHelper provides access to Google Cloud Storage APIs. You
/// can get an instance of this class via <c>StorageClientHelper.GetStorageClient()</c>.
/// </summary>
public sealed class StorageClientHelper : IFirebaseService
{
private StorageClient storageClient;

private StorageClientHelper(FirebaseApp app)
{
this.storageClient = StorageClient.Create(app.Options.Credential);
}

/// <summary>
/// Gets the StorageClient instance associated with the default Firebase app. Return value is
/// <c>null</c> if the default app doesn't yet exist.
/// </summary>
/// <returns>The <see cref="StorageClient"/> instance associated with the specified
/// app.</returns>
public static StorageClient GetStorageClient()
{
var app = FirebaseApp.DefaultInstance;
if (app == null)
{
return null;
}

return GetStorageClient(app);
}

/// <summary>
/// Returns the StorageClient instance for the specified app.
/// </summary>
/// <returns>The <see cref="StorageClient"/> instance associated with the specified
/// app.</returns>
/// <exception cref="System.ArgumentNullException">If the app argument is null.</exception>
/// <param name="app">An app instance.</param>
public static StorageClient GetStorageClient(FirebaseApp app)
{
if (app == null)
{
throw new ArgumentNullException("App argument must not be null.");
}

return app.GetOrInit<StorageClientHelper>(typeof(StorageClientHelper).Name, () =>
{
return new StorageClientHelper(app);
}).storageClient;
}

/// <summary>
/// Deletes this <see cref="StorageClientHelper"/> service instance.
/// </summary>
void IFirebaseService.Delete()
{
this.storageClient.Dispose();
}
}
}
1 change: 1 addition & 0 deletions FirebaseAdmin/FirebaseAdmin/FirebaseAdmin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Api.Gax" Version="2.7.0" />
<PackageReference Include="Google.Api.Gax.Rest" Version="3.2.0" />
<PackageReference Include="Google.Apis.Auth" Version="1.49.0" />
<PackageReference Include="System.Collections.Immutable" Version="1.7.1" />
Expand Down
6 changes: 6 additions & 0 deletions FirebaseAdmin/FirebaseAdmin/FirebaseApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
"3003684e85e61cf15f13150008c81f0b75a252673028e530ea95d0c581378da8c6846526ab9597" +
"4c6d0bc66d2462b51af69968a0e25114bde8811e0d6ee1dc22d4a59eee6a8bba4712cba839652f" +
"badddb9c")]
[assembly: InternalsVisibleToAttribute("FirebaseAdmin.IntegrationTests,PublicKey=" +
"002400000480000094000000060200000024000052534131000400000100010081328559eaab41" +
"055b84af73469863499d81625dcbba8d8decb298b69e0f783a0958cf471fd4f76327b85a7d4b02" +
"3003684e85e61cf15f13150008c81f0b75a252673028e530ea95d0c581378da8c6846526ab9597" +
"4c6d0bc66d2462b51af69968a0e25114bde8811e0d6ee1dc22d4a59eee6a8bba4712cba839652f" +
"badddb9c")]
namespace FirebaseAdmin
{
internal delegate TResult ServiceFactory<out TResult>()
Expand Down