You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.8 KiB

  1. /*
  2. * MinIO .NET Library for Amazon S3 Compatible Cloud Storage, (C) 2017 MinIO, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using Minio;
  17. using System;
  18. using System.Net;
  19. using System.Threading.Tasks;
  20. namespace FileUploader
  21. {
  22. /// <summary>
  23. /// This example creates a new bucket if it does not already exist, and uploads a file
  24. /// to the bucket.
  25. /// </summary>
  26. public class FileUpload
  27. {
  28. static void Main(string[] args)
  29. {
  30. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
  31. | SecurityProtocolType.Tls11
  32. | SecurityProtocolType.Tls12;
  33. var endpoint = "play.min.io";
  34. var accessKey = "Q3AM3UQ867SPQQA43P2F";
  35. var secretKey = "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG";
  36. try
  37. {
  38. var minio = new MinioClient(endpoint, accessKey, secretKey).WithSSL();
  39. Run(minio).Wait();
  40. }
  41. catch (Exception ex)
  42. {
  43. Console.WriteLine(ex.Message);
  44. }
  45. Console.ReadLine();
  46. }
  47. /// <summary>
  48. /// Task that uploads a file to a bucket
  49. /// </summary>
  50. /// <param name="minio"></param>
  51. /// <returns></returns>
  52. private static async Task Run(MinioClient minio)
  53. {
  54. // Make a new bucket called mymusic.
  55. var bucketName = "mymusic-folder"; //<==== change this
  56. var location = "us-east-1";
  57. // Upload the zip file
  58. var objectName = "my-golden-oldies.mp3";
  59. var filePath = "C:\\Users\\vagrant\\Downloads\\golden_oldies.mp3";
  60. var contentType = "application/zip";
  61. try
  62. {
  63. bool found = await minio.BucketExistsAsync(bucketName);
  64. if (!found)
  65. {
  66. await minio.MakeBucketAsync(bucketName, location);
  67. }
  68. await minio.PutObjectAsync(bucketName, objectName, filePath, contentType);
  69. Console.WriteLine("Successfully uploaded " + objectName);
  70. }
  71. catch (Exception e)
  72. {
  73. Console.WriteLine(e);
  74. }
  75. }
  76. }
  77. }