LoginSignup
14
1

要件

Kintoneで作業しているユーザー全てS3に保存しているオブジェクトを参照可能にしたい

方法

1. Access key IDとSecret access keyの作成

ここでは詳細述べませんが、AWS公式サイトで参考お願いします。
https://docs.aws.amazon.com/ja_jp/IAM/latest/UserGuide/id_credentials_access-keys.html#access-keys_required-permissions

2. IAMバケットポリシーを作成する

IAMバケットポリシーを以下のように作成します。

{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Effect": "Allow",
			"Action": "s3:*",
			"Resource": [
				"arn:aws:s3:::バケット名/*"
			],
			"Condition": {
				"IpAddress": {
					"aws:SourceIp": [
						"特定のIPアドレス"
					]
				}
			}
		}
	]
}

3. KintoneのJavaScript作成

準備

・AWSのSDKをしようするために、https://sdk.amazonaws.com/js/aws-sdk-2.1503.0.min.js を事前に「JavaScript / CSSでカスタマイズ」機能で導入する必要があります。
・またボタンを作成する際にkintone UI Component 使用しましたのでhttps://unpkg.com/kintone-ui-component@1.11.0/umd/kuc.min.jsも事前に導入する必要があります。

今回私は自分で作成したボタンの中で、AWSコマンドでAWS S3オブジェクトに訪問するようにしました、コードは以下となります。
事前に、AWSのURIを記入する用のフィールドとボタンを作成するためのスペースフィールドを用意する必要があります。(※オブジェクのURLではなくて、URIを使用してください)

kintone上のコード

(() => {
  'use strict';
  const Kuc = Kucs['1.11.0'];
  kintone.events.on('app.record.detail.show', (event) => {
    // ボタン要素を作成
    const field = kintone.app.record.getSpaceElement('事前に設置したスペースフィールド名');
    const button = new Kuc.Button({
      text: 'ボタン名',
      type: 'submit',
    });
    
    // Presigned URLを生成するためのパラメータのKeyを作成する
    const formatFieldValue = () => {
      const record = kintone.app.record.get().record;
      const fieldURI = ; // S3のURIを記入するフィールドレコード
      const Key = fieldURI.split( 's3://バケット名/' );
      return Key;
    };

    // ボタンがクリックされた時の処理
    button.addEventListener('click', () => {
      // 値を整形
      const formattedValue = formatFieldValue();

      // ここでフィールドの値を整形する処理を実装する

      //AWS SDKをインポートする
      const AWS = window.AWS;

      // AWS認証情報の設定
      AWS.config.update({
        accessKeyId: '作成したAccess key ID',
        secretAccessKey: '作成したSecret access key',
        region: 'ap-northeast-1' // AWSのリージョン
      });

      const s3 = new AWS.S3();
      // Presigned URLを生成するためのパラメータ
      const params = {
        Bucket: 'バケット名',
        Key: formattedValue[1],
        Expires: 10 // 有効期限(秒)(仮に10秒)
      };

      // Presigned URLの生成
      const url = s3.getSignedUrl('getObject', params, (err, url) => {
        if (err) {
          console.error('Error creating presigned URL:', err);
        } else {
          console.log('Generated presigned URL:', url);
          window.open(url);
          // ここで取得したURLを使用する処理を記述する(例えば、リンクを生成するなど)
        }
      });
    });

    // ボタンを詳細画面のフィールドに追加
    field.appendChild(button);
    return event
  });
})();

14
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
1