# AssetStaging

Stages a file or directory from a location on the file system into a staging directory.

This is controlled by the context key 'aws:cdk:asset-staging' and enabled
by the CLI by default in order to ensure that when the CDK app exists, all
assets are available for deployment. Otherwise, if an app references assets
in temporary locations, those will not be available when it exists (see
https://github.com/aws/aws-cdk/issues/1716).

The `stagedPath` property is a stringified token that represents the location
of the file or directory after staging. It will be resolved only during the
"prepare" stage and may be either the original path or the staged path
depending on the context setting.

The file/directory are staged based on their content hash (fingerprint). This
means that only if content was changed, copy will happen.

Extends: Construct › AssetStaging
Implements: IConstruct, IDependable

- Source: [`core/lib/asset-staging.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/core/lib/asset-staging.ts#L80)

## Example

```ts
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import * as cdk from 'aws-cdk-lib';

declare const dockerImage: cdk.DockerImage;
declare const localBundling: cdk.ILocalBundling;
const assetStaging = new cdk.AssetStaging(this, 'MyAssetStaging', {
  sourcePath: 'sourcePath',

  // the properties below are optional
  assetHash: 'assetHash',
  assetHashType: cdk.AssetHashType.SOURCE,
  bundling: {
    image: dockerImage,

    // the properties below are optional
    bundlingFileAccess: cdk.BundlingFileAccess.VOLUME_COPY,
    command: ['command'],
    entrypoint: ['entrypoint'],
    environment: {
      environmentKey: 'environment',
    },
    local: localBundling,
    network: 'network',
    outputType: cdk.BundlingOutput.ARCHIVED,
    platform: 'platform',
    securityOpt: 'securityOpt',
    user: 'user',
    volumes: [{
      containerPath: 'containerPath',
      hostPath: 'hostPath',

      // the properties below are optional
      consistency: cdk.DockerVolumeConsistency.CONSISTENT,
    }],
    volumesFrom: ['volumesFrom'],
    workingDirectory: 'workingDirectory',
  },
  exclude: ['exclude'],
  extraHash: 'extraHash',
  follow: cdk.SymlinkFollowMode.NEVER,
  ignoreMode: cdk.IgnoreMode.GLOB,
});
```

## Initializer

```ts
new AssetStaging(scope: Construct, id: string, props: AssetStagingProps)
```

## Construct Props

- `assetHash`: string — Specify a custom hash for this asset.

  **Default:** based on `assetHashType`

  If `assetHashType` is set it must
  be set to `AssetHashType.CUSTOM`. For consistency, this custom hash will
  be SHA256 hashed and encoded as hex. The resulting hash will be the asset
  hash.

  NOTE: the hash is used in order to identify a specific revision of the asset, and
  used for optimizing and caching deployment activities related to this asset such as
  packaging, uploading to Amazon S3, etc. If you chose to customize the hash, you will
  need to make sure it is updated every time the asset changes, or otherwise it is
  possible that some deployments will not be invalidated.

- `assetHashType`: [AssetHashType](/cdk/api/v2/docs/aws-cdk-lib.AssetHashType.html) — Specifies the type of hash to calculate for this asset.

  **Default:** the default is `AssetHashType.SOURCE`, but if `assetHash` is explicitly specified this value defaults to `AssetHashType.CUSTOM`.

  If `assetHash` is configured, this option must be `undefined` or
  `AssetHashType.CUSTOM`.

- `bundling`: [BundlingOptions](/cdk/api/v2/docs/aws-cdk-lib.BundlingOptions.html) — Bundle the asset by executing a command in a Docker container or a custom bundling provider.

  **Default:** uploaded as-is to S3 if the asset is a regular file or a .zip file, archived into a .zip file and uploaded to S3 otherwise

  The asset path will be mounted at `/asset-input`. The Docker
  container is responsible for putting content at `/asset-output`.
  The content at `/asset-output` will be zipped and used as the
  final asset.

- `exclude`: string[] — File paths matching the patterns will be excluded.

  **Default:** nothing is excluded

  See `ignoreMode` to set the matching behavior.
  Has no effect on Assets bundled using the `bundling` property.

- `extraHash`: string — Extra information to encode into the fingerprint (e.g. build instructions and other inputs).

  **Default:** hash is only based on source content

- `follow`: [SymlinkFollowMode](/cdk/api/v2/docs/aws-cdk-lib.SymlinkFollowMode.html) = SymlinkFollowMode.NEVER — A strategy for how to handle symlinks.

- `ignoreMode`: [IgnoreMode](/cdk/api/v2/docs/aws-cdk-lib.IgnoreMode.html) = IgnoreMode.GLOB — The ignore behavior to use for `exclude` patterns.

- `sourcePath`: string (required) — The source file or directory to copy from.

## Properties

- `absoluteStagedPath`: string — Absolute path to the asset data.

  If asset staging is disabled, this will just be the source path or
  a temporary directory used for bundling.

  If asset staging is enabled it will be the staged path.

  IMPORTANT: If you are going to call `addFileAsset()`, use
  `relativeStagedPath()` instead.

- `assetHash`: string — A cryptographic hash of the asset.

- `BUNDLING_INPUT_DIR`: string (static) — The directory inside the bundling container into which the asset sources will be mounted.

- `BUNDLING_OUTPUT_DIR`: string (static) — The directory inside the bundling container into which the bundled output should be written.

- `isArchive`: boolean — Whether this asset is an archive (zip or jar).

- `node`: [Node](/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `packaging`: [FileAssetPackaging](/cdk/api/v2/docs/aws-cdk-lib.FileAssetPackaging.html) — How this asset should be packaged.

- `sourcePath`: string — The absolute path of the asset as it was referenced by the user.

## Methods

- `clearAssetHashCache(): void` (static) — Clears the asset hash cache.

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `relativeStagedPath(stack: Stack): string` — Return the path to the staged asset, relative to the Cloud Assembly (manifest) directory of the given stack.

  Only returns a relative path if the asset was staged, returns an absolute path if
  it was not staged.

  A bundled asset might end up in the outDir and still not count as
  "staged"; if asset staging is disabled we're technically expected to
  reference source directories, but we don't have a source directory for the
  bundled outputs (as the bundle output is written to a temporary
  directory). Nevertheless, we will still return an absolute path.

  A non-obvious directory layout may look like this:

  ```
    CLOUD ASSEMBLY ROOT
      +-- asset.12345abcdef/
      +-- assembly-Stage
            +-- MyStack.template.json
            +-- MyStack.assets.json <- will contain { "path": "../asset.12345abcdef" }
  ```

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.

  **Parameters**

  - `mixins` — The mixins to apply.

  **Returns:** This construct for chaining
