예제 #1
0
        public void ModifyRequest_DefaultOptions()
        {
            var request = new InsertRequest(null, null, "project");
            var options = new CreateBucketOptions();

            options.ModifyRequest(request);
            Assert.Null(request.PredefinedAcl);
            Assert.Null(request.PredefinedDefaultObjectAcl);
            Assert.Null(request.Projection);
        }
예제 #2
0
        public void ModifyRequest_AllOptions()
        {
            var request = new InsertRequest(null, null, "project");
            var options = new CreateBucketOptions
            {
                PredefinedAcl = PredefinedBucketAcl.AuthenticatedRead,
                PredefinedDefaultObjectAcl = PredefinedObjectAcl.BucketOwnerFullControl,
                Projection = Projection.Full
            };

            options.ModifyRequest(request);
            Assert.Equal(PredefinedAclEnum.AuthenticatedRead, request.PredefinedAcl);
            Assert.Equal(PredefinedDefaultObjectAclEnum.BucketOwnerFullControl, request.PredefinedDefaultObjectAcl);
            Assert.Equal(ProjectionEnum.Full, request.Projection);
        }
예제 #3
0
파일: GoogleStorage.cs 프로젝트: mjb501/gcp
 public Bucket CreateBucket(string projectId, string bucketName, CreateBucketOptions options = null)
 {
     return(_wrappedClient.CreateBucket(projectId, bucketName, options));
 }
예제 #4
0
        public async Task <IActionResult> PostBookPdf(int userID, int bookID, IFormFile pdf)
        {
            if (pdf == 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;

                    storageClient.CreateBucket("assignment1-179919", "api_project_books");
                }
            }
            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 = pdf.FileName.Substring(pdf.FileName.LastIndexOf("."));

            fileName += fileType;

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

            await pdf.CopyToAsync(ms);

            var result = await storageClient.UploadObjectAsync("api_project_books", userID + "/" + bookID + "/" + fileName, pdf.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 { downloadURL = "https://storage.googleapis.com/api_project_books/" + userID + "/" + bookID + "/" + fileName }));
        }