Ejemplo n.º 1
0
        public async Task <OperationResult> CreateBucket(string key)
        {
            try
            {
                Bucket bucket = await _client.CreateBucketAsync(_projectID, key);

                return(new OperationResult(true, "Created bucket", HttpStatusCode.OK));
            }
            catch (GoogleApiException e)
            {
                return(new OperationResult(false, e.Message, (HttpStatusCode)e.Error.Code));
            }
        }
Ejemplo n.º 2
0
        public async Task <bool> CreateBucketAsync(string bucket)
        {
            try
            {
                await _client.CreateBucketAsync(_projectId, bucket);

                return(true);
            }
            catch (GoogleApiException e)
            {
                // Bucket already exist
                return(e.HttpStatusCode == HttpStatusCode.Conflict);
            }
        }
Ejemplo n.º 3
0
        private static async Task <Bucket> SetUpGCP(StorageClient client, Bucket bucket)
        {
            try
            {
                bucket = await client.CreateBucketAsync("YOURPROJECTID", "BUCKETNAME", new CreateBucketOptions { PredefinedAcl = PredefinedBucketAcl.PublicReadWrite });
            }
            catch (Google.GoogleApiException e)
                when(e.Error.Code == 409)
                {
                    // The bucket already exists.  That's fine.
                    bucket = await client.GetBucketAsync("BUCKETNAME");
                }

            return(bucket);
        }
Ejemplo n.º 4
0
        public override async Task <string> SaveAsync(FileSetOptions fileSetOptions)
        {
            GoogleCredential credential = await AuthorizedAsync(fileSetOptions);

            StorageClient storageClinet = await StorageClient.CreateAsync(credential);

            PredefinedObjectAcl predefinedObjectAcl = PredefinedObjectAcl.ProjectPrivate;
            PredefinedBucketAcl predefinedBucketAcl = PredefinedBucketAcl.ProjectPrivate;

            switch (fileSetOptions.FileAccess)
            {
            case FileAccessLevel._private:
                predefinedObjectAcl = PredefinedObjectAcl.AuthenticatedRead;
                predefinedBucketAcl = PredefinedBucketAcl.AuthenticatedRead;
                break;

            case FileAccessLevel._public:
                predefinedObjectAcl = PredefinedObjectAcl.PublicRead;
                predefinedBucketAcl = PredefinedBucketAcl.PublicRead;
                break;
            }


            if (fileSetOptions.folderOptions == FolderOptions.CreateIfNull)
            {
                var folder = await storageClinet.GetBucketAsync(fileSetOptions.Folder);

                if (folder == null)
                {
                    await storageClinet.CreateBucketAsync(fileSetOptions.ProjectId, fileSetOptions.Folder, new CreateBucketOptions()
                    {
                        PredefinedAcl = predefinedBucketAcl, PredefinedDefaultObjectAcl = predefinedObjectAcl
                    });
                }
            }

            fileSetOptions._stream.Position = 0;

            await storageClinet.UploadObjectAsync(fileSetOptions.Folder, fileSetOptions.Key, fileSetOptions.ContentType, fileSetOptions._stream, new UploadObjectOptions()
            {
                PredefinedAcl = predefinedObjectAcl
            });

            return(fileSetOptions.Key);
        }
Ejemplo n.º 5
0
        private async Task <bool> CreateBucketIfNotExists(StorageClient storage)
        {
            Bucket bucket = null;
            var    code   = -1;

            try
            {
                bucket = await storage.GetBucketAsync(IMAGES_BUCKET_NAME);
            }
            catch (GoogleApiException gae)
            {
                code = gae.Error.Code;
            }

            if (code == 404)
            {
                bucket = await storage.CreateBucketAsync("titan-185301", IMAGES_BUCKET_NAME);
            }

            return(bucket != null);
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> PostBookImage(int userID, int bookID, IFormFile image)
        {
            if (image == null)
            {
                return(BadRequest(new BadRequestResponse("Body is not in the correct format")));
            }

            // Instantiates a client.
            StorageClient storageClient = StorageClient.Create();

            try
            {
                // Creates the new bucket if does not exist.
                var bucket = await storageClient.GetBucketAsync("api_project_books");

                if (bucket == null)
                {
                    CreateBucketOptions createBucketOptions = new CreateBucketOptions();
                    createBucketOptions.Projection = Projection.Full;

                    await storageClient.CreateBucketAsync("assignment1-179919", "api_project_books", createBucketOptions);
                }
            }
            catch (Google.GoogleApiException e) when(e.Error.Code == 409)
            {
                // The bucket already exists. That's fine.
                Console.WriteLine(e.Error.Message);
            }

            MemoryStream ms = new MemoryStream();

            var fileName = Guid.NewGuid().ToString();

            var fileType = image.FileName.Substring(image.FileName.LastIndexOf("."));

            fileName += fileType;

            if (image.Length == 0)
            {
                return(BadRequest(new BadRequestResponse("file cannot be empty")));
            }

            await image.CopyToAsync(ms);

            var result = storageClient.UploadObjectAsync("api_project_books", userID + "/" + bookID + "/" + fileName, image.ContentType, ms);

            if (result == null)
            {
                try
                {
                    Book book = await Context.GetBookByID(bookID);

                    if (result == null)
                    {
                        return(NotFound(new DataNotFoundResponse("Book not found in database")));
                    }

                    Context.DeleteBook(bookID);

                    return(Ok());
                }
                catch (Exception e)
                {
                    return(BadRequest(new BadRequestResponse("Error deleting book")));
                }
            }

            return(Ok(new { imageURL = "https://storage.googleapis.com/api_project_books/" + userID + "/" + bookID + "/" + fileName }));
        }
Ejemplo n.º 7
0
 private async Task SetupClientAndBucketAsync()
 {
     //Creation of container
     await _client.CreateBucketAsync(_projectId, _bucketName);
 }