{"id":132,"date":"2024-04-07T18:24:33","date_gmt":"2024-04-07T22:24:33","guid":{"rendered":"https:\/\/coding101.xyz\/?p=132"},"modified":"2024-07-16T08:51:08","modified_gmt":"2024-07-16T12:51:08","slug":"access-aws-s3-with-go","status":"publish","type":"post","link":"https:\/\/coding101.xyz\/?p=132","title":{"rendered":"Access AWS S3 with Go"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Packages<\/h2>\n\n\n\n<p>The following packages are needed; there are currently two versions, if you write new code you should use the second version, as the first one will no longer be supported shortly<\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-code\"><code>\"github.com\/aws\/aws-sdk-go-v2\/aws\"\n\"github.com\/aws\/aws-sdk-go-v2\/config\"\n\"github.com\/aws\/aws-sdk-go-v2\/credentials\"\n\"github.com\/aws\/aws-sdk-go-v2\/service\/s3\"<\/code><\/pre>\n\n\n\n<p>Most of the packages are from s3, the rest support creating a client etc. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Endpoint<\/h2>\n\n\n\n<p>If you use AWS no endpoint info is needed. However for any other providers, endpoint must be specified, as, although they implement S3, obviously the endpoint does not have anything to do with Amazon&#8217;s<\/p>\n\n\n\n<p>Here is the endpoint for Linode: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>https:&#47;&#47;us-east-1.linodeobjects.com<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Coding<\/h2>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008000; font-weight: bold\">package<\/span> main\n\n<span style=\"color: #008000; font-weight: bold\">import<\/span> (\n<span style=\"color: #BA2121\">&quot;context&quot;<\/span>\n<span style=\"color: #BA2121\">&quot;fmt&quot;<\/span>\n<span style=\"color: #BA2121\">&quot;github.com\/aws\/aws-sdk-go-v2\/aws&quot;<\/span>\n<span style=\"color: #BA2121\">&quot;github.com\/aws\/aws-sdk-go-v2\/config&quot;<\/span>\n<span style=\"color: #BA2121\">&quot;github.com\/aws\/aws-sdk-go-v2\/credentials&quot;<\/span>\n<span style=\"color: #BA2121\">&quot;github.com\/aws\/aws-sdk-go-v2\/service\/s3&quot;<\/span>\n<span style=\"color: #BA2121\">&quot;os&quot;<\/span>\n)\n\n<span style=\"color: #008000; font-weight: bold\">func<\/span> main() {\nproceed()\n\n}\n\n<span style=\"color: #008000; font-weight: bold\">func<\/span> proceed() {\nctx <span style=\"color: #666666\">:=<\/span> context.Background()\n\ncfg, err <span style=\"color: #666666\">:=<\/span> \nclient <span style=\"color: #666666\">:=<\/span> s3.NewFromConfig(cfg, <span style=\"color: #008000; font-weight: bold\">func<\/span>(o <span style=\"color: #666666\">*<\/span>s3.Options) {\no.Region = <span style=\"color: #BA2121\">&quot;us-east-1&quot;<\/span>\no.BaseEndpoint = aws.String(<span style=\"color: #BA2121\">&quot;https:\/\/us-east-1.linodeobjects.com&quot;<\/span>)\naccessKey <span style=\"color: #666666\">:=<\/span> os.Getenv(<span style=\"color: #BA2121\">&quot;CURRENT_KEY&quot;<\/span>)\naccessSecret <span style=\"color: #666666\">:=<\/span> os.Getenv(<span style=\"color: #BA2121\">&quot;CURRENT_SECRET&quot;<\/span>)\no.Credentials = credentials.NewStaticCredentialsProvider(accessKey, accessSecret, <span style=\"color: #BA2121\">&quot;&quot;<\/span>)\n})\n\n<span style=\"color: #408080; font-style: italic\">\/\/ ok, list objects<\/span>\n\noutput, err <span style=\"color: #666666\">:=<\/span> client.ListObjectsV2(ctx, <span style=\"color: #666666\">&amp;<\/span>s3.ListObjectsV2Input{\nBucket: aws.String(<span style=\"color: #BA2121\">&quot;bucket-storage&quot;<\/span>),\n})\n\n<span style=\"color: #008000; font-weight: bold\">if<\/span> err <span style=\"color: #666666\">!=<\/span> <span style=\"color: #008000; font-weight: bold\">nil<\/span> {\nfmt.Println(<span style=\"color: #BA2121\">&quot;there was an error... &quot;<\/span>, err.Error())\n} <span style=\"color: #008000; font-weight: bold\">else<\/span> {\nfmt.Println(<span style=\"color: #BA2121\">&quot;list bucket with success&quot;<\/span>)\n<span style=\"color: #008000; font-weight: bold\">for<\/span> _, object <span style=\"color: #666666\">:=<\/span> <span style=\"color: #008000; font-weight: bold\">range<\/span> output.Contents {\nfmt.Println(<span style=\"color: #666666\">*<\/span>object.Key)\n}\n}\n\n<span style=\"color: #408080; font-style: italic\">\/\/ ok, now upload the object<\/span>\nfileName <span style=\"color: #666666\">:=<\/span> os.Getenv(<span style=\"color: #BA2121\">&quot;FILE_NAME&quot;<\/span>)\nopen, err <span style=\"color: #666666\">:=<\/span> os.Open(fileName)\n<span style=\"color: #008000; font-weight: bold\">if<\/span> err <span style=\"color: #666666\">!=<\/span> <span style=\"color: #008000; font-weight: bold\">nil<\/span> {\nfmt.Println(<span style=\"color: #BA2121\">&quot;there was an error opening the file&quot;<\/span>)\n}\n_, err = client.PutObject(ctx, <span style=\"color: #666666\">&amp;<\/span>s3.PutObjectInput{\nBucket: aws.String(<span style=\"color: #BA2121\">&quot;bucket-storage&quot;<\/span>),\nKey:    aws.String(<span style=\"color: #BA2121\">&quot;current.txt&quot;<\/span>),\nBody:   open,\n})\n\n<span style=\"color: #008000; font-weight: bold\">if<\/span> err <span style=\"color: #666666\">!=<\/span> <span style=\"color: #008000; font-weight: bold\">nil<\/span> {\nfmt.Println(<span style=\"color: #BA2121\">&quot;error putting the object into the bucket&quot;<\/span>)\n}\n\n<span style=\"color: #408080; font-style: italic\">\/\/ now get the file<\/span>\ngetOutput, err <span style=\"color: #666666\">:=<\/span> client.GetObject(ctx, <span style=\"color: #666666\">&amp;<\/span>s3.GetObjectInput{\nBucket: aws.String(<span style=\"color: #BA2121\">&quot;bucket-storage&quot;<\/span>),\nKey:    aws.String(<span style=\"color: #BA2121\">&quot;current.txt&quot;<\/span>),\n})\n\n<span style=\"color: #008000; font-weight: bold\">if<\/span> err <span style=\"color: #666666\">!=<\/span> <span style=\"color: #008000; font-weight: bold\">nil<\/span> {\nfmt.Println(<span style=\"color: #BA2121\">&quot;error: %w&quot;<\/span>, err)\n} <span style=\"color: #008000; font-weight: bold\">else<\/span> {\nb <span style=\"color: #666666\">:=<\/span> <span style=\"color: #008000\">make<\/span>([]<span style=\"color: #B00040\">byte<\/span>, <span style=\"color: #666666\">1000<\/span>)\ninfo, _ <span style=\"color: #666666\">:=<\/span> getOutput.Body.Read(b)\nfmt.Println(fmt.Sprintf(<span style=\"color: #BA2121\">&quot;read: %d bytes that are &gt;&gt;&gt;%s&lt;&lt;&lt;&quot;<\/span>, info, <span style=\"color: #008000\">string<\/span>(b[<span style=\"color: #666666\">0<\/span>:info])))\n}\n\n<span style=\"color: #408080; font-style: italic\">\/\/ and now delete the file<\/span>\n_, err = client.DeleteObject(ctx, <span style=\"color: #666666\">&amp;<\/span>s3.DeleteObjectInput{\nBucket: aws.String(<span style=\"color: #BA2121\">&quot;bucket-storage&quot;<\/span>),\nKey:    aws.String(<span style=\"color: #BA2121\">&quot;current.txt&quot;<\/span>),\n})\n\n<span style=\"color: #008000; font-weight: bold\">if<\/span> err <span style=\"color: #666666\">!=<\/span> <span style=\"color: #008000; font-weight: bold\">nil<\/span> {\nfmt.Println(fmt.Errorf(<span style=\"color: #BA2121\">&quot;error encountered %w&quot;<\/span>, err))\n} <span style=\"color: #008000; font-weight: bold\">else<\/span> {\nfmt.Println(<span style=\"color: #BA2121\">&quot;deleted...&quot;<\/span>)\n}\n}\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Packages The following packages are needed; there are currently two versions, if you write new code you should use the second version, as the first one will no longer be supported shortly<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-132","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=132"}],"version-history":[{"count":5,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/132\/revisions"}],"predecessor-version":[{"id":154,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/132\/revisions\/154"}],"wp:attachment":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}