Skip to content

Reference

Akira Kashihara edited this page May 14, 2022 · 9 revisions

Clam

Clam class is an entry point to your application or service.

Constructor

Clam(name = "GCS");
  • @param {string} [name="GCS"] - Put "GCS" if you use Google Cloud Storage

Usage

Clam(params: JSON)

Make new instance of Clam with params. If you do not set name argument, Clam connects to Google Cloud Storage as default.
params is a json setting value to connect Google Cloud Storage.

Example
const params = {
  clientId: [Client ID you get on Google API Console],
  redirectUrl: [Redirect URL Google OAuth gives access token],
  scope: [Scope you set on Google API Console],
}

const clam = new Clam(params);

Clam(params: JSON, name: string)

You can select upload service using Clam but you can select only Google Cloud Storage on current latest version. If you set name, the following code is a sample code.

Example
const params = {
  clientId: [Client ID you get on Google API Console],
  redirectUrl: [Redirect URL Google OAuth gives access token],
  scope: [Scope you set on Google API Console],
}

const clam = new Clam(params, "GCP");

Method

Clam class is an entry point to your application or service.

setCred(params)

setCred set credential information as instance variable.

  • @param {JSON} params - Setting parameter using authorization. This must include 'clientId', 'redirectUrl' and 'scope'
  • @param {string} params.clientId - You get client ID from Google Cloud Platform API / Credentials.
  • @param {string} params.redirectUrl - Redirect URL. It will get autorized information from Google.
  • @param {string} params.scope - Scope is access permission level. Please refer to OAuth2.0 scope on Google Developer.

getAuth()

getAuth provide auth page to each storage service.

No params and returns

getAuthInfo()

getAuthInfo gets the information to access to storage service from redirect URL.

  • @returns {JSON} Return value is JSON which includes the information to access to storage service.

uploadFiles(fileList: Object, bucketName: string)

uploadFiles uploads files you selected to storage service.

  • @param {Object} fileList - fileList is from
  • @param {string} bucketName - The bucket name you will upload file to.
  • @param {() => void} callback - The callback function. 1st argument is an error. 2nd argument is file name.
  • @returns {JSON} - Return JSON includes file name list of each result; success or failed.

GCS

GCS is an API which connects between your application and Google Cloud Platform.

Constructor

GCS(toolKit)
  • @param {ToolKit} toolKit - An instance of ToolKit ("./toolkit")

Usage

GCS(toolKit)

Make new instance of GCS with params.

Example
const params = {
  clientId: [Client ID you get on Google API Console],
  redirectUrl: [Redirect URL Google OAuth gives access token],
  scope: [Scope you set on Google API Console],
}

const gcs = new GCS(toolKit);

Method

setCred(params)

Example

const params = {
  clientId: [Client ID you get on Google API Console],
  redirectUrl: [Redirect URL Google OAuth gives access token],
  scope: [Scope you set on Google API Console],
}

const gcs = new GCS(toolKit);
gcs.setCred(params);

#scopeCheck(scope)

#scopeCheck is a method to check whether an argument is valid scope or not.

  • @param {string} scope
  • @returns {Boolean} true is valid, false is invalid

oauthSignIn()

oauthSignIn gets an access token to access to Google Cloud Storage you want to upload file through OAuth2.0.
The access token will be set to the redirect URL.

oauthSignIn has no params and returns.

getAccessToken(location: Object)

getAccessToken gets the access token to access to Google Cloud Storage from URL string which is set to the redirect URL by Google Auth.
It returns the access token, and sets it to an instance variable of GCS.

  • @param {Object} location - 'location' is set after OAuth2.0.
  • @returns {string} Access token to access to Google Cloud Storage

Example

const gcs = new GCS(params);
const location = location;
const token = gcs.getAccessToken(location);

uploadFiles(fileList: Object, bucketName: string, accessToken: string?)

uploadFiles uploads files user selected using .

  • @param {Object} fileList - fileList is from
  • @param {string} bucketName - The bucket name you will upload file to.
  • @param {() => void} callback - callback function
  • @param {string} accessToken - The access token to access Google Cloud Storage
  • @returns {JSON} "ok" has the file list that was successfully uploaded. "ng" has the file list that was failed to upload.

Example

<input type="file" id="file-upload" name="file-upload" multiple />

</body>
<script type="module">

...

const gcs = new GCS(params);

...

const files = document.getElementById("file-upload").files;
const result = await gcs.uploadFiles(files, [Bucket Name], callback);
</script>

uploadFile(event)

uploadFile is a method to upload a file.

  • @param {*} event - This parameter is set by addEventListener
  • - Bind Parameters -
    • @param {string} bucketName - The bucket name you will upload a file.
    • @param {File} file - The file you will upload
    • @param {string} token - The access token you got in authorization
    • @param {Function} callback - The callback function. 1st argument is an error message. 2nd argument is the file name you will upload.