This is the 31st day of my participation in the August Text Challenge.More challenges in August

Previous post: “Using Golang to delete specified buckets (part 1)”

preface

In the previous article, “Using Golang language to implement the specified bucket deletion method (1)” introduced the verification logic before deleting the bucket, today we will take a look at the specific logical processing when deleting the bucket. Through the introduction of the two articles basically delete the bottom of the bucket source code to make it clear.

The body of the

Let’s get right to the point, there are about three layers of logic required when deleting buckets. We will introduce the specific situation separately next.

The first layer

A bucket usually contains stored files. Before deleting a bucket, delete the stored files first. We query the database to see which storage files are contained in the bucket and then delete them one by one according to the query results.

Code implementation: first call ListObjectsV2 () query stored files, and then call remove interface RemoveObject ().

The second floor

The bucket may also contain files that are being uploaded. For example, large files are generally uploaded in fragments and may stop in the middle of uploading. When deleting storage files, consider deleting these intermediate files as well. Similarly, we query the database for unfinished upload records and delete intermediate temporary files based on these query results.

Code implementation: first call ListIncompleteUploads () to query the unfinished file upload, then call RemoveIncompleteUpload interface.

The third layer

After deleting all the files and temporary files in the bucket, you can delete the bucket itself. Finally, delete the bucket record from the database.

Code implementation: call interface RemoveBucket to delete the bucket.

Logical summary plus code implementation

Define the delete method: cleanupBucket, with the bucket name and type as a string. The specific logic code is as follows:

func cleanupBucket(bucketName string) error {
	// Create coroutines
	doneCh := make(chan struct{})
	// Clear resources after exit
	defer close(doneCh)
	// Iterate over the query results
	for objCh := range ListObjectsV2(bucketName, "".true, doneCh) {
		ifobjCh.Err ! =nil {
			return objCh.Err
		}
		ifobjCh.Key ! ="" {
                        // Delete the files in the bucket
			err := RemoveObject(bucketName, objCh.Key)
			iferr ! =nil {
				return err
			}
		}
	}
        // Query the files that have not been uploaded
	for objPartInfo := range ListIncompleteUploads(bucketName, "".true, doneCh) {
		ifobjPartInfo.Err ! =nil {
			return objPartInfo.Err
		}
		ifobjPartInfo.Key ! ="" {
                        // Delete incomplete upload files
			err := RemoveIncompleteUpload(bucketName, objPartInfo.Key)
			iferr ! =nil {
				return err
			}
		}
	}
	// Finally, delete the bucket itself
	err := RemoveBucket(bucketName)
	iferr ! =nil {
		return err
	}
	return err
}
Copy the code

Ok, so that completes the implementation of the underlying code logic for deleting buckets. After reading, is there a lot of points before their own did not think of.

At the end

Does it feel like discovering a new world by learning the underlying code to delete buckets? Although we don’t usually implement this code ourselves, there’s a lot to learn from the ideas. That’s all for today’s content. I’m Liuzhen007, welcome to leave a comment and “three links with one button”.

digression

I have to say something else today. If you are careful, you will notice that I have updated my articles every day this month, and attached a screenshot at the end of each article. Today is the last day of this month, August more text challenge, today even completed. Ha ha, specially in this record. Today is the end of an activity, but also the starting point of a new activity, continue to come on! The force to!

Calendar Clocking (August Challenge)