Skip to content

Commit c9ff78b

Browse files
committed
Add .NET image upload sample
1 parent ff20c24 commit c9ff78b

9 files changed

+589
-0
lines changed

examples/dotnet-image-upload/.gitignore

Lines changed: 477 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using CloudinaryDotNet;
2+
using CloudinaryDotNet.Actions;
3+
4+
Account account = new Account(
5+
Environment.GetEnvironmentVariable("CLOUDINARY_CLOUD_NAME"),
6+
Environment.GetEnvironmentVariable("CLOUDINARY_API_KEY"),
7+
Environment.GetEnvironmentVariable("CLOUDINARY_API_SECRET"));
8+
9+
var cloudinary = new Cloudinary(account);
10+
11+
var images = new List<string>
12+
{
13+
"./images/aldebaran-s-uXchDIKs4qI-unsplash.jpg",
14+
"./images/brian-mcgowan-I0fDR8xtApA-unsplash.jpg",
15+
"./images/spacex-OHOU-5UVIYQ-unsplash.jpg",
16+
"./images/spacex-VBNb52J8Trk-unsplash.jpg",
17+
"./images/stellan-johansson-1PP0Fc-KSd4-unsplash.jpg",
18+
"https://images.unsplash.com/photo-1451187863213-d1bcbaae3fa3?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=880&q=80"
19+
};
20+
21+
foreach(var image in images)
22+
{
23+
var uploadParameters = new ImageUploadParams()
24+
{
25+
File = new FileDescription(image)
26+
};
27+
var uploadResult = await cloudinary.UploadAsync(uploadParameters);
28+
Console.WriteLine($"Successfully uploaded {image}");
29+
Console.WriteLine($"> Result: {uploadResult.SecureUrl}");
30+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Uploading Images to Cloudinary with the .NET SDK
2+
3+
## 🧰 Uploading with the SDK in a .NET project
4+
5+
To use the SDK in a .NET project, first install the SDK:
6+
7+
```sh
8+
dotnet add package CloudinaryDotNet
9+
```
10+
Then, add the necessary package reference
11+
12+
```csharp
13+
using CloudinaryDotNet;
14+
using CloudinaryDotNet.Actions;
15+
```
16+
17+
Finally, configure a new instance of Cloudinary:
18+
19+
```csharp
20+
Account account = new Account(
21+
"<Your Cloud Name>",
22+
"<Your API Key>",
23+
"<Your API Secret>");
24+
25+
Cloudinary cloudinary = new Cloudinary(account);
26+
```
27+
28+
Finally, run the upload method, passing in your image source:
29+
30+
```csharp
31+
var uploadParams = new ImageUploadParams()
32+
{
33+
File = new FileDescription("<Image Location or URL>")
34+
};
35+
var uploadResult = await cloudinary.UploadAsync(uploadParams);
36+
```
37+
38+
## 🚀 Get Started with This Example
39+
40+
* Add the following environment variables via your shell
41+
```sh
42+
# In Bash
43+
export CLOUDINARY_CLOUD_NAME=<Your Cloud Name>
44+
export CLOUDINARY_API_KEY=<Your API Key>
45+
export CLOUDINARY_API_SECRET=<Your API Secret>
46+
47+
# Or
48+
49+
# In CMD
50+
set CLOUDINARY_CLOUD_NAME="<Your Cloud Name>"
51+
set CLOUDINARY_API_KEY="<Your API Key>"
52+
set CLOUDINARY_API_SECRET="<Your API Secret>"
53+
```
54+
55+
* Install the project dependencies with:
56+
57+
```sh
58+
dotnet restore
59+
```
60+
61+
* Run the program with:
62+
63+
```sh
64+
dotnet run
65+
```
66+
67+
The program will run in your terminal and you'll see the URL results logged.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<RootNamespace>dotnet_image_upload</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="CloudinaryDotNet" Version="1.22.0" />
13+
</ItemGroup>
14+
15+
</Project>
670 KB
Loading
5.26 MB
Loading
640 KB
Loading
757 KB
Loading
2.84 MB
Loading

0 commit comments

Comments
 (0)