Skip to content

Instantly share code, notes, and snippets.

@yyano
Last active April 21, 2022 02:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yyano/a3090a342edeb5ab1c9b45dd75f0dadc to your computer and use it in GitHub Desktop.
Save yyano/a3090a342edeb5ab1c9b45dd75f0dadc to your computer and use it in GitHub Desktop.
AWS Lambdaにて、S3からPDFファイルを/tmpにダウンロード。そして、/tmpからS3へアップロード
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.S3;
using Amazon.S3.Model;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace AWSLambda20171015
{
public class Function
{
/// <summary>
/// A simple function that takes a string and does a ToUpper
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public async Task<string> FunctionHandler(string input, ILambdaContext context)
{
AmazonS3Client client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2);
//S3のPDFファイルを/tmpにコピー
GetObjectRequest requestGet = new GetObjectRequest
{
BucketName = "example-bucket-name",
Key = "input.pdf"
};
GetObjectResponse responseGet = await client.GetObjectAsync(requestGet);
var filename = String.Format("{0}.{1}", DateTime.Now.ToString("yyyyMMddHHmmss"), "input.pdf");
CancellationToken cancellationToken;
await responseGet.WriteResponseStreamToFileAsync("/tmp/"+filename, false, cancellationToken).ConfigureAwait(false);
string[] files = Directory.GetFiles("/tmp/");
foreach (string s in files)
{
Console.WriteLine(s);
}
// /tmpのファイルをS3にコピー
var stream = new FileStream("/tmp/" + filename, FileMode.Open, FileAccess.Read, FileShare.Read);
PutObjectRequest request = new PutObjectRequest()
{
BucketName = "example-bucket-name",
Key = filename,
ContentType = "application/pdf",
InputStream = stream,
//FilePath = filename
};
try
{
await client.PutObjectAsync(request);
Console.WriteLine( "OK");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
return input?.ToUpper() + "/abc";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment